Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get selected grid row values using this code but some errors occur.

C#
 protected void list_products_RowCommand(object sender, GridViewCommandEventArgs e)
{
     int RowIndex_ = int.Parse(e.CommandArgument.ToString());

     for (int i = 0; i < list_products.Rows.Count; i++)
        {
            if (RowIndex_ == i)
            {

                GridViewRow selectedRow = list_products.SelectedRow;
                product objproduct = new product();
                objproduct.userid = Convert.ToString(Session["id"]);
                this.Label5.Text = list_products.SelectedRow.Cells[2].Text;
                objproduct.productid = list_products.SelectedRow.Cells[2].Text;
                objproduct.product_name = list_products.SelectedRow.Cells[3].Text;
                objproduct.product_quantity = list_products.SelectedRow.Cells[4].Text;
                objproduct.product_type = list_products.SelectedRow.Cells[5].Text;

            }
            else
            { this.Label4.Text = "cant be select"; }
}
Posted
Updated 22-Jul-10 5:03am
v3
Comments
E.F. Nijboer 22-Jul-10 7:03am    
and what would this error be? and do you know where? which line?
Sandeep Mewara 22-Jul-10 7:05am    
Use PRE tags to format code part. It makes the question readable.
Sunasara Imdadhusen 22-Jul-10 8:12am    
Please provide error description, so I can easily identify where you are wrong :)
am.net 22-Jul-10 9:28am    
I think u r in wrong way, u r accessing e.CommandArgument and want to access row index, how is possible.
first u sure that u send something with e.CommandArgumen.
access e.rowindex, will help u.
Dalek Dave 22-Jul-10 11:04am    
Edited for minor spelling.
Also, am.net do not use text speak, it is rubbish.

1 solution

here is the grid also see the error message at the end..please also you can suggest me better solution and for updating row in inline editing...
thanks in advance




<asp:GridView ID="list_products" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="5"
Width="929px" AutoGenerateSelectButton="True"
onselectedindexchanged="list_products_SelectedIndexChanged"
onrowcommand="list_products_RowCommand"
onrowdatabound="list_products_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="select" runat="server" CommandName="Select" CommandArgument='<%#Eval("product_id")%>' >Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id"><EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("product_id")%>' Visible="false"></asp:TextBox></EditItemTemplate>
<ItemTemplate><asp:Label ID="Label11" runat="server" Text='<%#Eval("product_id")%>'></asp:Label></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Product Name"><EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("product_name")%>' Visible="false"></asp:TextBox></EditItemTemplate>

<ItemTemplate><asp:Label ID="Label12" runat="server" Text='<%#Eval("product_name")%>'></asp:Label></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Quantity"><EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("quantity")%>' Visible="false"></asp:TextBox></EditItemTemplate>
<ItemTemplate><asp:Label ID="Label13" runat="server" Text='<%#Eval("quantity")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Type"><EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("type")%>' Visible="false"></asp:TextBox></EditItemTemplate>
<ItemTemplate><asp:Label ID="Label14" runat="server" Text='<%#Eval("type")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>


</Columns>

<AlternatingRowStyle BackColor="#FF99CC" />

</asp:GridView>

-------------------------------
error::


Error 9 'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?) D:\Projectz\attendace\Products.aspx.cs 72 64 D:\Projectz\attendace\
 
Share this answer
 
v2
Comments
Sandeep Mewara 22-Jul-10 12:14pm    
Reason for my vote of 1
Not an answer. Should have used 'Improve Question' to edit/update it.
Shining Legend 23-Jul-10 0:16am    
First of all you will get selected row for gridview only if the command name is select. moreover do you have datakey names set for the girdview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900