Tag Archives: Gridview

Select and edit a record in GridView in vb.net

If you want to select a record in a Gridview in VB.Net and then edit that record in a separate page, then you can use the following code to give you an idea.

I assume that you alredy know how to add a gridview and populate it using vb.net

You would need to add a hyperlink field in the gridview columns as shown belo

The headertext will show what will appear in the header row
datatextfield will be name of your datafield which is the primary key for the data
datanavigateurlformatstring  – this field will construct the name of your page which the system will navigate to if someone clicks on the edit button
datanavigateurlfields  is again the primary key eg. My primary key was “NewsId”
DataTextFormatString  tells what will be displayed on the data gridl eg. I want to show a hyperlink called “Edit”

so whenever someone clicks on the edit button the system will take you to the “EditNews.aspx?q=1 where 1 will be the NewsID

<asp:hyperlinkfield headertext=”Edit” datatextfield=”NewsID” datanavigateurlformatstring=”EditNews.aspx?q={0}” datanavigateurlfields=”NewsID” DataTextFormatString=”Edit” Text=”Edit” />

Here is the complete code which shows where the hyperlink code will sit

<asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True”
AllowSorting=”True” AutoGenerateColumns=”False” DataKeyNames=”NewsID”
DataSourceID=”SqlDataNews” Width=”617px”>

<Columns>
<asp:BoundField DataField=”NewsID” HeaderText=”NewsID” InsertVisible=”False”
ReadOnly=”True” SortExpression=”NewsID” />

<asp:BoundField DataField=”NewsDate” HeaderText=”NewsDate”
SortExpression=”NewsDate” />
<asp:BoundField DataField=”NewsStartDate” HeaderText=”NewsStartDate”
SortExpression=”NewsStartDate” />
<asp:BoundField DataField=”NewsEndDate” HeaderText=”NewsEndDate”
SortExpression=”NewsEndDate” />
<asp:BoundField DataField=”NewsShortDescripton”
HeaderText=”NewsShortDescripton” SortExpression=”NewsShortDescripton” />

<asp:hyperlinkfield headertext=”Edit”
datatextfield=”NewsID”

datanavigateurlformatstring=”EditNews.aspx?q={0}”
datanavigateurlfields=”NewsID” DataTextFormatString=”Edit” Text=”Edit” />

</Columns>

</asp:GridView>