双击C#中的datagridview打开新的2按钮,如更新和删除 [英] Doubleclick on datagridview in C# open new from with 2 button like update and delete

查看:54
本文介绍了双击C#中的datagridview打开新的2按钮,如更新和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();





我尝试了什么:



我试过这个。如果我双击datagrid中的一行,一个新表单附带两个按钮更新和删除。如果我点击更新按钮,其他表格也打开,但数据输入的内容没有在第一个表单datagrid中更新。



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.

推荐答案

我可以推荐使用 BindingSource ,请参阅:详细的数据绑定教程 [ ^ ]

您可以为所有表单共享 BindingSource ,如果更新,绑定的数据网格也将更新。
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.


这篇关于双击C#中的datagridview打开新的2按钮,如更新和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆