Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am loading data from my database file to Datagrid. Doubleclick on a row in datagridview in c#, open new from with 2 button like update and delete. if I select update, which record I selected in datagrid with that one new form should open. I am not getting idea how to do that. Here is my code

loading datagrid with a database file 


con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM[dbo].    [itemDB]", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        foreach (DataRow item in dt.Rows)
        {
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[0].Value = item["itmcode"].ToString();
            dataGridView1.Rows[n].Cells[1].Value = item["brname"].ToString();
            dataGridView1.Rows[n].Cells[2].Value = item["itmname"].ToString();
            dataGridView1.Rows[n].Cells[3].Value = item["itmunit"].ToString();
            dataGridView1.Rows[n].Cells[4].Value = item["itmgr"].ToString();
            dataGridView1.Rows[n].Cells[5].Value = item["itmml"].ToString();
            dataGridView1.Rows[n].Cells[6].Value = item["itmpc"].ToString();
            dataGridView1.Rows[n].Cells[7].Value = item["itmtx"].ToString();
            dataGridView1.Rows[n].Cells[8].Value = item["itmbyp"].ToString();
            dataGridView1.Rows[n].Cells[9].Value = item["itmdlrmrg"].ToString();
            dataGridView1.Rows[n].Cells[10].Value = item["itmrtmrg"].ToString();
            dataGridView1.Rows[n].Cells[11].Value = item["itmdlrp"].ToString();
            dataGridView1.Rows[n].Cells[12].Value = item["itmmrp"].ToString();
            dataGridView1.Rows[n].Cells[13].Value = item["itmusrcode"].ToString();
            dataGridView1.Rows[n].Cells[14].Value = item["active"].ToString();



        }
        con.Close();


What I have tried:

I tried this. if I double click on one row in datagrid one new form came with two buttons update and delete. If I click on update button other form also opened but data what i entered in that not updating in first form datagrid.
Posted
Updated 15-Mar-17 21:45pm

1 solution

I can recommend using a BindingSource, see: A Detailed Data Binding Tutorial[^]
You can then share the BindingSource for all your forms, if it is updated the bound datagrids will be updated too.
 
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