将数据从DataGrid添加到表单 [英] Adding Data from a DataGrid to a Form

查看:111
本文介绍了将数据从DataGrid添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#Windows窗体程序中,我有两种形式:1种是Form,另一种是Form2
Form是我将数据放在文本框中的位置,而Form2是数据网格的位置,该数据网格显示了一个表,其中插入了我的数据.现在,如何将表格中的数据放入表单?
我在Form2上的代码

In my C# windows form program, i have two forms: 1 is Form and the other one is Form2
the Form is where I put data on the textboxes and the Form2 is where is a datagrid wich shows a table with the data i inserted. Now, how do I put data from a table to a form?
My code on Form2

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        DataSet ds = new DataSet();
        private object counter;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string a, b;
            string local_novo = "C:/Users/Paulo Isabel/Desktop/Nova pasta/novo";
            SqlConnection Mycon = new SqlConnection(@"Server=(PauloIsabel-PC); Data Source=PAULOISABEL-PC;Initial Catalog=master;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter();
            SqlDataAdapter da1 = new SqlDataAdapter();

            Mycon.Open();
            a = ID01.Text + "-" + ID02.Text;
            b = Nome01.Text;

            da.SelectCommand = new SqlCommand("Select * From Cidadão where ID=('" + a + "') OR NomeCompleto like('" + b +    "%') OR ID=('" + a + "') AND NomeCompleto like('" + b + "%')", Mycon);
            da.Fill(ds);
         
            DG.DataSource = ds.Tables[0];
            Mycon.Close();
        }
}



顺便说一句,我希望显示在文本框中的数据



and btw, i want the data shown in the textboxes

推荐答案

我认为可以做到这一点.
Form 中,创建了DataTable 语句,并用TextBoxes中的值填充了DataTable1 .
Form2 中,将DataGridView 的可见性(可访问性)设置为public.
然后在FormButton1 Click 事件中

I think something like this can be done.
In Form a DataTable say DataTable1 is created and it is populated with the values from the TextBoxes.
In Form2 make the visibility (accessibility) of DataGridView as public.
Then in the Click event of Button1 of Form

Form2 form2 = new Form2();
Form2.DataGridView1.DataSource = DataTable1;
Form2.ShowDialog();
Form2.Dispose();


这篇关于将数据从DataGrid添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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