I want to display my table columns and data in datagridview at run time when user select table name I know that i have to set bindingsourse property . But i dont know how to do programming for that can you tell me.

You mean table as DataTable? DataTable cannot be bind to dgv control. You can only populate dgv with using DataRow object, like for instance:

dataGridView1.Columns.Add("col1", "ID");
            dataGridView1.Columns.Add("col2", "NAME");
            string[] array1 = new string[] { "one", "two", "three" };

            table = new DataTable("myTable");
            table.Columns.Add(new DataColumn("id", typeof(int)));
            table.Columns.Add(new DataColumn("name", typeof(string)));
           
            DataRow dr;
            for (int i = 0; i < array1.Length; i++)
            {
                dr = table.NewRow();
                dr["id"] = i.ToString();
                dr["name"] = array1[i];
                table.Rows.Add(dr);
            }

            int row = 0;
            foreach (DataRow dr1 in table.Rows)
            {
                dataGridView1.Rows.Add();
                dataGridView1[0, row].Value = dr1[0].ToString();
                dataGridView1[1, row].Value = dr1[1].ToString();
                row++;
            }

If you want o use Binding property, you have to use some other object, like BindingList. See this example:

private struct Test
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

        private BindingList<Test> list = new BindingList<Test>();

        public Form1()
        {
            InitializeComponent();
            dataGridView1.DataSource = list;
            string[] array1 = new string[] { "one", "two", "three" };

            for (int i = 0; i < array1.Length; i++)
                list.Add(new Test { ID = i, Name = array1[i] });
        }

Hope this helps explaining the difference which object you can and which you cannot directly bind to some controls. But there is plenty of objects you can use as a binding source (like arrays ([]), lists,...).
Mitja

I understand the concept.
But.
I have create my table at run time. when new table is create then the data of that table show in datagridview. for that I have use dgv if their is any another control to do this task please tell me.

What the problem is ? are you getting any error ? show us your some efforts that what you have done so far ?

I understand the concept.
But.
I have create my table at run time. when new table is create then the data of that table show in datagridview. for that I have use dgv if their is any another control to do this task please tell me.

I have create my table at run time. when new table is create then the data of that table show in datagridview. for that I have use dgv if their is any another control to do this task please tell me.

Depends from where you get data?
If this is from dataBase, the best option for sure is DataTable.
If not, there is plentiy better objects to use, like generic BindingList, or generic List.

Tell me whats your source of data?

I use this code to show my data form table in datagridview:

on form load() event

cn.ConnectionString = "Data Source=WELL\\SQLEXPRESS;Initial Catalog=hire;Integrated Security=True";
            cn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand("select * from cash", cn);
            da.SelectCommand = cmd;
            da.Fill(ds);
            BindingSource bnd = new BindingSource();
            bnd.DataSource = cmd;
            DataGridView dv = new DataGridView();
            dv.DataSource = bnd;

but this code does not show any error but not even data i dont know how to fix this problem. i think now u can understand what i want to do.

I believe the problem is with this "bnd.DataSource = cmd;". You need to assign your "ds.Tables[0]" to DataSource property of BindingSource object.

Also i suggest you don't need to use BindingSource class to bind your gridview. You can directly assign your dataset object to DataSource property of Gridview..

See the below modified code :

cn.ConnectionString = "Data Source=WELL\\SQLEXPRESS;Initial Catalog=hire;Integrated Security=True";
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select * from cash", cn);
da.SelectCommand = cmd;
da.Fill(ds);
[B]DataGridView dv = new DataGridView();
dv.DataSource = ds.Tables[0];[/B]

try by this and let me know...

I use this code to show my data form table in datagridview:

on form load() event

cn.ConnectionString = "Data Source=WELL\\SQLEXPRESS;Initial Catalog=hire;Integrated Security=True";
            cn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand("select * from cash", cn);
            da.SelectCommand = cmd;
            da.Fill(ds);
            BindingSource bnd = new BindingSource();
            bnd.DataSource = cmd;
            DataGridView dv = new DataGridView();
            dv.DataSource = bnd;

but this code does not show any error but not even data i dont know how to fix this problem. i think now u can understand what i want to do.

thanks! my problem is solved....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.