Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click GridView Find Selected Row

I'm trying to take a GridView and get back the data from the row that was clicked. I've tried the code below and when I click the row I get back 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 grid.

<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 row data bound I make sure that the click should 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 this gets fired which I can put a breakpoint on the first line and I see the index of what I clicked on get stored in a. However when I get to the foreach it skips right past it because it shows GridView1 having a Count of 0 rows. In theory it should have a couple hundred rows and when the index matches it should grab the data in the 6th cell over and store it in string b. Why am I getting no rows on the 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;
}
like image 909
John Wesley Gordon Avatar asked Jan 07 '14 21:01

John Wesley Gordon


People also ask

How can we get selected row data from GridView in ASP NET?

When a row is selected in a GridView control, use the SelectedRow property to retrieve the GridViewRow object that represents that row. This is the same as retrieving the GridViewRow object at the index specified by the SelectedIndex property from the Rows collection.

How do you select data from GridView?

You will need to set the Bound column in order to see the text in the cells of the GridView control. In the above GridView Properties set 'AutoGenerateSelectButton=True'. See the following image of a GridView after setting 'AutoGenerateSelectButton=True'. Select the GridView and press F4 for the property window.

How do you get the data of a selected row from a grid in Extjs?

getSelection()[0]; var row = grid. store. indexOf(selectedRecord); you have to get the selected record of your grid and from that, you can search this record from your store and get its index.


Video Answer


1 Answers

Try just grabbing the row from the SelectedRow property:

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

It's my understanding that the Rows collection doesn't get repopulated on PostBacks when you're using those data source controls (like SqlDataSource).

You could probably use your existing code if you called .DataBind() on your GridView prior to trying to iterate through the Rows:

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

But that seems a little hacky.


After seeing your Page_Load, I see that you need to wrap your databinding code in an if(!Page.IsPostBack) block. Databinding on every postback is interrupting the process of ASP.NET maintaining the state of your controls via the ViewState.

like image 136
Josh Darnell Avatar answered Oct 14 '22 14:10

Josh Darnell