Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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
Posted
Updated 27-Apr-12 3:47am
v4
Comments
Prasad_Kulkarni 27-Apr-12 7:39am    
Where's the problem?
What you've tried so far? Have you tried anything, post your code snippet.
Prasad_Kulkarni 27-Apr-12 8:12am    
That's good!
Now tell me where & what error you're getting?
PointBreaker 27-Apr-12 9:39am    
there is no errors, i just want to know how to pass data from a table on the dataGrid to a textbox in a diferent form

1 solution

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

C#
Form2 form2 = new Form2();
Form2.DataGridView1.DataSource = DataTable1;
Form2.ShowDialog();
Form2.Dispose();
 
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