Edit Update in Windows Forms DataGridView Control using C#

shrawan
 
on Feb 26, 2013 01:50 AM
18913 Views

Dear Sirs,

 

I want to update/edit the data reflects in Gridview instead of creating a form to edit on C# windows form.

below is my code to reflect data in Gridview.

 

private void AInvoice_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server = SHRAWAN; iNITIAL cATALOG = mba;Integrated Security = false");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select InvoiceNo,AInvoiceNo,AInvoiceDate,SelectOption1 from CIDetail", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].HeaderText = "Commercial Invoice No";
dataGridView1.Columns[1].HeaderText = "A Invoice No";
dataGridView1.Columns[2].HeaderText = "A Invoice Date";
dataGridView1.Columns[3].HeaderText = "Select to Generate";

}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
on Feb 26, 2013 07:22 AM

You can try this

 private void FrmSaveDataFromDataGridView_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            dataGridView1.AllowUserToAddRows = false;

            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            btn.HeaderText = "Update";
            dataGridView1.Columns.Add(btn);
            btn.UseColumnTextForButtonValue = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int currentRowIndex = dataGridView1.CurrentCell.RowIndex;
            int currentColumnIndex = dataGridView1.CurrentCell.ColumnIndex;

            if (currentColumnIndex == dataGridView1.Columns.Count - 1)
            {
                currentColumnIndex = 0;

                string productId = dataGridView1[currentColumnIndex, currentRowIndex].Value.ToString();
                currentColumnIndex++;

                string productName = dataGridView1[currentColumnIndex, currentRowIndex].Value.ToString();
                currentColumnIndex++;

                string productPrice = dataGridView1[currentColumnIndex, currentRowIndex].Value.ToString();
                currentColumnIndex++;


                //HERE YOU CAN WRITE YOUR LOGIC TO SAVE THE DATA IN DATABASE
            }
        }

above is the sample code in which i have taken one button column in the datagrid view.

Based on that button click i get the data of that particular row in the variables

You can try the above codes

and to enter the data directly in the column you need to double click the particular cell. It will allow you to change the data of that particular cell directly. It does not have any seperate property