Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if(e.CommandName=="Select")
           {
               Int16 num = Convert.ToInt16(e.CommandArgument);

               txt_Name.Text = GridView1.Rows[num].Cells[2].Text;
               txt_Email.Text = GridView1.Rows[num].Cells[3].Text;
               txt_Phone.Text = GridView1.Rows[num].Cells[4].Text;
               ddl_Gender.Text = GridView1.Rows[num].Cells[5].Text;
               txt_DOB.Text = GridView1.Rows[num].Cells[6].Text;
           }
       }
Posted
Updated 17-Sep-15 20:45pm
v2
Comments
Naveen.Sanagasetti 18-Sep-15 2:50am    
Hi Rohit,

Can you please debug and see the CommandArgument, what it's return and check whether that is your desired RowIndex or not.?

I guess the problem is RowIndex, if RowIndex also correct then check your column type whether that is BoundField or TemplateField.

Please check and revert back to me.
Rohitk2409 18-Sep-15 2:54am    
<asp:GridView ID="GridView1" runat="server" BackColor="#CCFFFF" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False" HorizontalAlign="Center" OnRowCommand="GridView1_RowCommand" CellSpacing="1">
<footerstyle backcolor="#99CCCC" forecolor="#003399">
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<rowstyle backcolor="White" forecolor="#003399">
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<sortedascendingcellstyle backcolor="#EDF6F6">
<sortedascendingheaderstyle backcolor="#0D4AC4">
<sorteddescendingcellstyle backcolor="#D6DFDF">
<sorteddescendingheaderstyle backcolor="#002876">
<columns>
<asp:CommandField ShowSelectButton="True" />
<asp:TemplateField HeaderText="Id">
<itemtemplate>
<asp:Label ID="ID" runat="server" Text='<%#Eval("Id") %>'>'>


<asp:TemplateField HeaderText="Name">
<itemtemplate>
<asp:Label ID="Name" runat="server" Text='<%#Eval("Name") %>'>'>


<asp:TemplateField HeaderText="Email">
<itemtemplate>
<asp:Label ID="Email" runat="server" Text='<%#Eval("Email") %>'>'>


<asp:TemplateField HeaderText="Phone">
<itemtemplate>
<asp:Label ID="Phone" runat="server" Text='<%#Eval("Phone") %>'>'>


<asp:TemplateField HeaderText="Gender">
<itemtemplate>
<asp:Label ID="Gender" runat="server" Text='<%#Eval("Gender") %>'>'>


<asp:TemplateField HeaderText="Date of Birth">
<itemtemplate>
<asp:Label ID="DOB" runat="server" Text='<%#Eval("DOB") %>'>'>


<asp:TemplateField HeaderText="Image">
<itemtemplate>
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("Image") %>' Height="100" Width="100" />



Rohitk2409 18-Sep-15 3:04am    
I debug the code, null is showing in all field when I checked what value is going to any field
add commandargument in code.

eg.
<asp:LinkButton ID="LinkButton2" runat="server" Text="ID" CommandArgument='<%# Eval("id") %>' CommandName="Select">

here i have used linkbutton , on click it will call OnRowCommand event and u will get id.

if you want to do select/delete and record then use as same as above and change its CommandName. when you want to select use "Select" and when you want to delete any record use commandname as "Delete".

if this solution is worked for you tell me.

Thank you.
Rohitk2409 18-Sep-15 3:19am    
thnk you, bt its nt working :(

Hi Rohit,

You can add an button on each row and on click button you can get the selected values.

ASP.NET
<asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkDelete" Text="Delete" CssClass="itemDelete" CommandArgument='<%# Eval("Value") %>'
                                    runat="server" OnClick="DeleteFile" />
                            </ItemTemplate>
                        </asp:TemplateField>


and then create button click event and get the values
 
Share this answer
 
Hi Rohit,

As per your comment, you said you are using TemplateFields then how you are calling control using cells, which is not correct.

The Problem in above code is calling control.

Use below sample,

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if(e.CommandName=="Select")
   {
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

        txt_Name.Text = (row.FindControl("Name") as Label).Text;
        txt_Email.Text = (row.FindControl("Email") as Label).Text;
        txt_Phone.Text = (row.FindControl("Phone") as Label).Text;
        ddl_Gender.Text = (row.FindControl("Gender") as Label).Text;
        txt_DOB.Text = (row.FindControl("DOB") as Label).Text;
    }
}


and design your gridview column as suggested by Vikas, before implement the task you should read some articles which related to your requirement after that start work on that.

There are no of mistakes in your design as well as your code, use proper naming conventions whenever you are using label give labelname as "lbl" with prefix, I gave solution as per your design, but this is not a good way to give control names.

Hope you understood..
 
Share this answer
 

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