Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows
hi,
 
I have a datatable as
 
 public DataTable createdatatable()
        {
            dt = new DataTable("mydt");
            dt.Columns.Add("id",System.Type.GetType("System.Int32"));
            
            dt.Columns.Add("name".ToString());
            
            dt.Columns.Add("add".ToString());
            dt.Columns.Add("age", System.Type.GetType("System.Int32"));
          //  dt.Columns.Add("age","System.DateTime");
            dt.Columns.Add("email".ToString());
            dt.Columns.Add("dob", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("tel".ToString());
            
            return dt;
           }
and I want to bind that datatable to datagridview as
 private void button1_Click(object sender, EventArgs e)
        {  
            DataTable dt1 = new DataTable();
            dr = dt.NewRow();
            dr["name"] = textBox2.Text;
            dr["add"] = textBox3.Text;
            dr["age"] =Convert.ToInt32(textBox4.Text);
            dr["email"] = textBox7.Text;
            dr["dob"] =Convert.ToDateTime(textBox5.Text);
            dr["tel"] =textBox6.Text;
            dt.Rows.Add(drow);
            dataGridView1.DataSource = dt;
            ?????????????????????what should i write here
 
       }
Posted 7-Mar-11 8:38am
Edited 7-Mar-11 9:02am
Henry Minute223.8K
v2
Comments
orc_orc_orc at 7-Mar-11 13:46pm
   
dataGridView1.DataBind(); ?
sahuanil at 7-Mar-11 13:54pm
   
i want to bind it in window application,i am not finding databind() method in window application.please help me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

?????????????????????what should i write here
Nothing!
There is no Databind() in winforms. It's not needed. It would work without it.
 
In Winforms, alternatively, you can populate a binding source with a datatable and then assign that binding source to the datagridview.
 

I would suggest you to pick some sample tutorials online to look at winforms implementation and learn a little before starting to code on Winforms.
 
Look at the Datagridview class and sample codes related to it at the bottom of this link: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx[^]
http://msdn.microsoft.com/en-us/library/fbk67b6z%28v=VS.90%29.aspx[^]
 
For samples, to start with look at these: Winforms DataGrid samples and examples[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What is it that you are expecting to have to write?
 
Setting the DataSource of the DataGridView is all that you should need to do.
 
Is your project giving errors or in some way not behaving as expected?
 
BTW: For Windows Forms there is no DataBind(), so don't waste your time searching for it. Smile | :) It comes from the GridView in the wonderful world of Web programming.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this
 dataGridView1.DataSource = dt.DefaultView;
Read this MSDN article DataGridView.DataSource Property[^], You will get idea on what is mean by dataGridView1.DataSource property. what are the different options to bind data to DataGridView.
 
Correct solution for your question is
 
datatable is
 public DataTable createdatatable()
        {
            DataTable dt = new DataTable("mydt");
            dt.Columns.Add("id", System.Type.GetType("System.Int32"));
            dt.Columns.Add("name".ToString());
            dt.Columns.Add("add".ToString());
            dt.Columns.Add("age", System.Type.GetType("System.Int32"));
            //  dt.Columns.Add("age","System.DateTime");
            dt.Columns.Add("email".ToString());
            dt.Columns.Add("dob", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("tel".ToString());
            return dt;
        }
Bind that datatable to datagridview as
private void button1_Click(object sender, EventArgs e)
        {
            DataRow dr;
            DataTable dt1 = createdatatable();
            dr = dt1.NewRow();
            dr["name"] = "test";
            dr["add"] = "test";
            dr["age"] = 1;
            dr["email"] = "test";
            dr["dob"] = Convert.ToDateTime("03/02/2011");
            dr["tel"] = "test";
            dt1.Rows.Add(dr);
            dataGridView1.DataSource = dt1.DefaultView;
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

When you try to add data into datagridview you need to utilize the DataSource property and provide a source which has data to be displayed. Here you're trying to add the DataSet as source but DataSet directly does not store the data into itself. Inside DataSet there are various tables (not always unless you explicitly name and create them). Hence, solution is to give the index number of the table stored inside the Dataset.
 
So, instead of using this:
 
DataGridView1.DataSource = ds;
 

Give the index number like this:
DataGridView1.DataSource = ds.Tables[0];
 

This must display the data correctly if you've not created multiple tables inside your DataSet. If you have multiple tables, then you need to give appropriate index number inside the square bracket where I've given 0 in above code.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100