Click GridView Find Selected Row

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 = //My secret string strSelect = "SELECT columnnames from tablenames where c in (@c) SqlDataSource1.SelectParameters.Clear(); SqlDataSource1.SelectCommand = strSelect; SqlDataSource1.SelectParameters.Add("c", c); try { GridView1.DataBind(); } catch (Exception e) { } GridView1.AutoGenerateColumns = true; } 
+6
source share
1 answer

Try just grab the string from the SelectedRow property:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView1.SelectedRow; string b = row.Cells[6].Text; } 

I understand that the Rows collection does not get repopulated on PostBacks when you use these data source controls (e.g. SqlDataSource).

You could probably use your existing code if you called .DataBind() in your GridView before trying to iterate through the rows:

 GridView1.DataSourceID="SqlDataSource1"; GridView1.DataBind(); 

But that seems a bit hacked.


After looking at your Page_Load, I see that you need to wrap the data binding code in an if(!Page.IsPostBack) . Data binding on each postback interrupts the ASP.NET process that maintains the state of your controls through the ViewState.

+6
source

All Articles