Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!
i have a problem is.. i dont know handles event when i clicking on button in datagridview, i design my form like below picture

http://nj4.upanh.com/b5.s25.d1/1b608c451153dc01b7dd697ba98e7643_41785524.nhchupmanhinh20120308145028.png[^]

i want when i clicked on the button then it show the infor of that row. Please reply me soon or mail me with Email: [removed]
Posted
Updated 7-Mar-12 20:56pm
v2
Comments
walterhevedeich 8-Mar-12 2:57am    
I've removed your email address. If someone knows the answer, they will be posting it here, not on your email.

Please, look at your screen.

Why the heck do you want a button on each and every row which does exactly the same thing, but for a different row of data?
All the buttons are doing is wasting screen real-estate and making it look clumsy.

Why not just show the data for the currently selected row? That way the user doesn't have to click on a button at all - when the selected row changes, the information is automatically updated.
Or, if you are bringing up a dialog to show the data, consider a single button which shows the data for the selected row? Again, it reduces the screen footprint, and looks less "busy". It's also pretty simple for the user to work out - the button is always in the same place, rather than moving with the row.
 
Share this answer
 
If you are using the TemplateField object and have to access the row index in the event handler method, set the button's CommandArgument property to an expression that identifies the current row. Sample code is:

XML
<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server"
      CommandName="AddToData"
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate>
</asp:TemplateField>


Create a method for the RowCommand event of the GridView control as:
C#
protected void GridView1_RowCommand(object sender,
  GridViewCommandEventArgs e)
{
  if (e.CommandName == "AddToData")
  {
    // Add code here to related to the row.
  }

  }
 
Share this answer
 
Ok i using vb.net not c# :d i testing hanldes event on datagridview :d
 
Share this answer
 
i have just complete :d it run ok. this is my code.

VB
Private Sub gvView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvView.CellContentClick
        Dim strID As String
        Dim strName As String
        Dim strPricw As String
        Dim strQuan As String
        If e.ColumnIndex = 0 Then
            strID = gvView.Rows(e.RowIndex).Cells(3).Value.ToString()
            strName = gvView.Rows(e.RowIndex).Cells(4).Value.ToString()
            strPricw = gvView.Rows(e.RowIndex).Cells(5).Value.ToString()
            strQuan = gvView.Rows(e.RowIndex).Cells(6).Value.ToString()
            MessageBox.Show("You has choosen: " & strID & " | " & strName & " | " & strPricw & " | " & strQuan)
        End If
    End Sub
 
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