Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Evening,
I have add button in DataGridView dynamically(c# Windows Application). Now I want to write some code on click of that button can anyone tell me how can I do that? Its urgent
Thanks
Posted
Comments
Santoshrohin 13-Feb-12 7:16am    
No one there?????
Code 89 13-Feb-12 7:23am    
can you post some of your code, so that we can understand exact error with this..
Rajeev Jayaram 13-Feb-12 7:33am    
Why are you in such a hurry? Are you paying us for this task?
Santoshrohin 13-Feb-12 7:33am    
This is my code
string s = "select * from tbinvoicedetails left join tbinvoices ON(tbinvoices.invoiceid=tbinvoicedetails.invoiceid) where " + cond;
//DataSet ds_cert_list = SqlCommonFunctions.getdatausingquery("select * from tbinvoicedetails where invoiceid = (select top 1 invoiceid from tbinvoices where qid=" + qid + " order by invoiceid desc)");
DataSet ds_cert_list = SqlCommonFunctions.getdatausingquery(s);

dataGridView1.RowCount = ds_cert_list.Tables[0].Rows.Count + 1;
dataGridView1.ColumnCount = 4;
dataGridView1.ColumnHeadersVisible = true;
dataGridView1.RowHeadersVisible = true;
dataGridView1[0, 0].Value = "Product";
dataGridView1[1, 0].Value = "Invoice Number";
dataGridView1[2, 0].Value = "Quantity";
dataGridView1[3, 0].Value = "Rate/Item";
// dataGridView1[4, 0].Value = "Total Cost";

double total_amount = 0;
for (int i = 0; i < ds_cert_list.Tables[0].Rows.Count; i++)
{

DataSet ds_templatename = SqlCommonFunctions.getdatausingquery(" select productcode as Product_Code, calibrationcharges as Calibration_Charges,invoicenumber,numberofcert from tbinvoicedetails left join tbproducts ON(tbproducts.subcategoryid=tbinvoicedetails.subcatid) where subcategoryid=" + ds_cert_list.Tables[0].Rows[i][3]);
//where subcategoryid=" + ds_cert_list.Tables[0].Rows[i][3]);
dataGridView1[0, i + 1].Value = ds_templatename.Tables[0].Rows[i][0];
dataGridView1[1, i + 1].Value = ds_templatename.Tables[0].Rows[i][2].ToString();
dataGridView1[2, i + 1].Value = ds_templatename.Tables[0].Rows[i][3];
dataGridView1[3, i + 1].Value = ds_templatename.Tables[0].Rows[i][1];

}

var buttonCol = new DataGridViewButtonColumn();
buttonCol.Name = "ButtonColumnName";
buttonCol.HeaderText = "Header";
buttonCol.Text = "Button Text";

dataGridView1.Columns.Add(buttonCol);
Santoshrohin 13-Feb-12 7:36am    
No Rajeev I am in hurry because I have to finish 2 3 tasks more before I log out. So please don't get misunderstand.

Hello

you can add btn into datagrid view try code as below you will definitely got success


VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "ID"
        DataGridView1.Columns(1).Name = "Name"
        DataGridView1.Columns(2).Name = "Price"

        Dim row As String() = New String() {"1", "P1", "1000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"2", "P2", "2000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"3", "P3", "3000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"4", "P4", "4000"}
        DataGridView1.Rows.Add(row)

        Dim btn As New DataGridViewButtonColumn()
        DataGridView1.Columns.Add(btn)
        btn.HeaderText = "Click Data"
        btn.Text = "Click Here"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True

    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = 3 Then
            MsgBox(("Row : " + e.RowIndex.ToString & "  Col : ") + e.ColumnIndex.ToString)
        End If
    End Sub
 
Share this answer
 
You need to use DataGridView.CellContentClick Event to detect button clicks for a DataGridViewButtonCell,

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx[^]
 
Share this answer
 
v3

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