Hi! I have some project to send my boss tomorrow, but I can't solve it by myself. I try to solve it for a long time. but I still can't.

I have a gridview to retrieve some data from database to show user. what job that they don't complete it. and i use a select link to selected on it. but when you choose it. I don't know how to use or how to get data from gridview to system know what are they seleced on it.

I want you help!
thanks.

Dani AI

Generated

Nice to see got it working and prompting a clearer question. For future readers: the simplest, most reliable pattern in ASP.NET Web Forms is to give the GridView a DataKeyNames value and use the grid's selection events to read the key and any row controls. That avoids brittle column-index lookups and survives reordering or template fields.

Example workflow (short):

  • Set DataKeyNames="YourPrimaryKey" on the GridView.
  • Add a select button (AutoGenerateSelectButton="true" or a ButtonField/LinkButton with CommandName="Select").
  • Handle SelectedIndexChanged to read GridView.DataKeys[GridView.SelectedIndex].Value and GridView.SelectedRow.FindControl(...) for template controls.
  • Alternatively handle RowCommand for custom command buttons and use e.CommandArgument + DataKeys.

Small markup + code samples:

<asp:GridView ID="gv" runat="server" DataKeyNames="JobID" OnSelectedIndexChanged="gv_SelectedIndexChanged">
  <Columns>
    <asp:BoundField DataField="JobName" HeaderText="Job" />
    <asp:ButtonField CommandName="Select" Text="Select" />
  </Columns>
</asp:GridView>
protected void gv_SelectedIndexChanged(object sender, EventArgs e)
{
    int id = Convert.ToInt32(gv.DataKeys[gv.SelectedIndex].Value);
    GridViewRow row = gv.SelectedRow;
    string jobName = row.Cells[0].Text; // or use FindControl for TemplateField
    // use id to load details, update DB, redirect, etc.
}

Troubleshooting tips:

  • Make sure DataKeyNames exactly matches a field in the data source.
  • Avoid rebinding the GridView on every PostBack; wrap initial binding in if (!IsPostBack).
  • Use FindControl when columns are TemplateFields; Cells[index] can break if columns change.
  • If using custom buttons, ensure CommandArgument supplies the row index (or use Container.DataItemIndex in templates).

Microsoft docs for selected events and datakeys are helpful: GridView.SelectedIndexChanged and GridView.DataKeys.

Recommended Answers

All 2 Replies

Sorry, Your question is not very clear. Try and simplify it for ease of understanding.

Hi! I have some project to send my boss tomorrow, but I can't solve it by myself. I try to solve it for a long time. but I still can't.

I have a gridview to retrieve some data from database to show user. what job that they don't complete it. and i use a select link to selected on it. but when you choose it. I don't know how to use or how to get data from gridview to system know what are they seleced on it.

I want you help!
thanks.

thanks a lot
I can solve it already
Hoorey!

Sorry, Your question is not very clear. Try and simplify it for ease of understanding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.