I am trying to take a GridView and return data from the row that was clicked. I tried the code below, and when I click on the row, I return the selected index, but when I look at the actual rows in the GridView, they show empty. Not sure what I am missing.
.Asp make my mesh.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" CssClass="datatables" Width="100%" DataSourceID="SqlDataSource1" GridLines="None" ShowFooter="True" AllowSorting="True" onrowcreated="GridView1_RowCreated" onrowdatabound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True" onrowcommand="GridView1_RowCommand" onselectedindexchanged="GridView1_SelectedIndexChanged"> <HeaderStyle CssClass="hdrow" /> <RowStyle CssClass="datarow" /> <PagerStyle CssClass="cssPager" /> </asp:GridView>
On each line of data binding, I have to make sure that the click has to set the selected index.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); } }
Then, when the selected index changes by clicking on it, it lights up, and I can put a breakpoint on the first line, and I see that the index of what I clicked gets in a. However, when I get to foreach, it slips right past it, because it shows GridView1, which has a graph of 0 rows. Theoretically, it should have a couple of hundred rows, and when the index matches it, it should capture the data in the 6th cell and save it in row b. Why am I not getting rows in a click?
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { int a = GridView1.SelectedIndex foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex == a) { b = row.Cells[6].Text; } } }
Here is my page load.
protected void Page_Load(object sender, EventArgs e) { c = HttpContext.Current.Session["c"].ToString(); SqlDataSource1.ConnectionString =