Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have form which has datagridview having 4 columns 3 textcolumns and 4th button column , i want to get all 3 columns text of same row when i click button column button of perticular row.

thnaks.
Posted
Comments
Thava Rajan 14-Oct-15 9:42am    
is it in windows forms or web forms make it clear?
Member 11543226 15-Oct-15 0:55am    
it is windows form application

1 solution

You can fire the
C#
OnRowCommand
event of GridView. On row command get the clicked row through
C#
CommandSource
property as following way -

XML
<asp:GridView runat="server" ID="grd1" AutoGenerateColumns="False" OnRowCommand="grd1_OnRowCommand">
               <Columns>
                   <asp:BoundField HeaderText="Code" DataField="Code" />
                   <asp:BoundField HeaderText="Name" DataField="Name" />
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Button runat="server" ID="gridBtn" Text="Click" CommandArgument="Get" />
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>



C#
protected void grd1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            var gvRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            var col1 = gvRow.Cells[0].Text;
            var col2 = gvRow.Cells[1].Text;
        }
 
Share this answer
 
Comments
Member 11543226 15-Oct-15 1:01am    
these events doesnt find in c# windows form application ? any replacements to this event
Anupam Shukla 2009 15-Oct-15 1:52am    
If you are using Windows From application, then you can invoke cell click event directly

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewButton cell = (DataGridViewButtonCell)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

// Any additional logic you want
}
Member 11543226 15-Oct-15 2:15am    
did the same found answer as well.
Member 11543226 15-Oct-15 2:15am    
thanks .
Anupam Shukla 2009 15-Oct-15 3:53am    
Accept the solution if this working for you!!

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