Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I have a datatable as
C#
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
C#
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
Updated 7-Mar-11 8:02am
v2
Comments
Orcun Iyigun 7-Mar-11 13:46pm    
dataGridView1.DataBind(); ?
software_Engi08 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

?????????????????????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[^]
 
Share this answer
 
v2
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. :) It comes from the GridView in the wonderful world of Web programming.
 
Share this answer
 
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
C#
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
C#
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;
        }
 
Share this answer
 
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.
 
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