How to set focus on last row of grid - c#

I need to set focus on the last row of the grid after a grid row updation takes place. Can anyone help me out on this?
Below is my gridview,
<asp:GridView ID="grdViewForComment" runat="server" OnRowDataBound="Row_DataBound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Advanced search">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Check adding below code
grdViewForComment.Rows[19].Focus(); // RowIndex here.
grdViewForComment.Rows.Count-1

Related

Add different Buttons on a single column of a GridView

I've added an Edit button to the Action column of my Gridview using a TemplateField. I want every column to have the edit button in the first column except for the last row, which should have an Add button there instead. How would I implement this separate last row with the different button? Here is the code I have.
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
</ItemTemplate>
</asp:TemplateField>
DataTable dt = new DataTable();
dt.Columns.Add("Action");
DataRow row1 = dt.NewRow();
...
dt.Rows.Add(row1);
gridRoles.DataSource = dt;
gridRoles.DataBind();
First you have to add 2 button 1st Edit(visible) and 2nd Add (make it display=none initially)
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="AddClick()" />
</ItemTemplate>
</asp:TemplateField>
Then Find Gridview Last row and find the Button Controls
GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
Button Edit = ((Button)row.FindControl("btnedit")).Text;
Button Add = ((Button)row.FindControl("btnadd")).Text;
Edit.visible =false;
Add.Visible =true;
Here reverse the button display option : False for Edit and true for Add
That item template column is going to add whatever it's contents are to every row. One way around this is to add and empty column, and add your button during the OnRowDataBound event.
You can check to see if you are on the last row, and if so, add the other button, rather than the edit button.
One way to do it is to use the footer as the last line of the GridView. It could look like this:
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" ... >
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" text="Add" OnClientClick="btnAddClick()" />
</FooterTemplate>
</asp:TemplateField>
....
</asp:GridView>
You can look at this article for a complete example, with input controls and validation: Inserting a New Record from the GridView's Footer.

Hyperlink in datagrid

I am inserting record in datagrid by binding it with data table and I had set autogenerate field ON for datagrid. Now all I want is first column which will comes in datagrid should be hyperlink field and when I will click on link it should redirect to another page. Please help
do like :
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
Edit:
You need to change the column type to a Hyperlink column.
and where to set is your job Sandesh
<Columns>
<asp:hyperlinkfield headertext="NewsHeadline"
datatextfield="NewsHeadline"
datanavigateurlfield="NewsURL"
datanavigateurlformatstring="http://{0}" />
</Columns>

How to Set Grid View Header Text As I Am Attaching list to Grid?

I am trying to add list in grid view but cant set its header text it is showing "item" in its text how to set its header text as my code is,
GridView1.DataSource = M5h;
GridView1.DataBind();
I also tried it with template column but it is also not working then i remove add directly add list as datasource
it is showing like,
item
0.5
0.6
12
.36
0.46
0.8
0.9
11
2.1
1.23
hopes for your suggestion thanks in advace
EDITED:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Try it
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="YourColumnDataField" HeaderText="item"/>
</Columns>
</asp:GridView>
you should define header tag.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Item //HeaderName
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="YourColumnDataField" HeaderText="item"/>
</Columns>

how to add a link button in the grid view by visual studio?

I want to add a link button in grid view that will navigate to the next form in the website. but i want to know how to insert that button into the grid view column? please help me..
You can add Link buttons in the Item template of a gridview.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Link" runat="server" PostBackUrl="MyNextPage.aspx">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
// TEXT FOR COLUMN NAME
</HeaderTemplate>
<ItemTemplate>
// ADD LINK BUTTON HERE.
</ItemTemplate>
</asp:TemplateField>
</Columns>
Add this inside grid view.
try this
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:ButtonField ButtonType="Link" HeaderText="LinkButton"/>
</Columns>
</asp:GridView>
Make Sure your GridViews datasource is bound. Set Auto-Generate Columns to false.
Open GridViewTask , add new column with Hyperlink Field proper name and path . You are done.

How can I show fewer than n columns in a grid view where it has n columns?

I have an ASP Grid View; its Datasource is a list of entities. The entity has 11 properties, but I want to show only some of the columns in the grid view.
My Grid View is
<asp:GridView runat="server" ID="GridForResult"
Caption="Update The Result for the Folloing Students"
Visible="true" ShowHeader="false">
<Columns>
<asp:TemplateField ItemStyle-Width="100px">
<ItemTemplate>
<%#Eval("TestRoll")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="120px">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<%#Eval("Program")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my code behind page, I have bound the grid like this:
List<FormGridEntity> gridEntities = new List<FormGridEntity>();
gridEntities = AdmissionResult_BAO.GetAllCandidateAdmissionInfo();
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
//What Will I need to do here or somewhere else
What should I do now?
#V4Vendetta has it right - in your GridView markup, set AutoGenerateColumns="false":
<asp:GridView runat="server" ID="GridForResult" AutoGenerateColumns="false" Caption="Update The Result for the Folloing Students" Visible="true" ShowHeader="false">
Alternatively, if you want to define columns in the markup and turn them off individually, you can set the Visible="false" property:
<asp:TemplateField ItemStyle-Width="100px" Visible="false">
You don't have to do anything in your code-behind, unless you want to turn columns on/off programatically:
GridForResult.Columns[0].Visible = false; // Turns off the first column

Categories