Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i want to enter the entries of previous form in next form datagridview.
i tried out this coding on form load event of second form:
C#
Program.adpt = new System.Data.SqlClient.SqlDataAdapter("select * from visitor_details", Program.con);
            DataSet ds;
            ds = new DataSet();
            Program.adpt.Fill(ds);
            for (int i = 0; i < 100; i++)
            {
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["name"].ToString();
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["company"].ToString();
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["phone"].ToString();
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["visitor_type"].ToString();
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["in_time"];
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["host_name"].ToString();
                dataGridView1.DataSource = ds.Tables[0].Rows[i]["initials"].ToString();

But i get an error "there is no row at position 0".
can anybody sort this problem out.
thanxs in advance
neaS
Posted
Updated 7-Aug-11 22:02pm
v2

Try this.
Program.adpt = new System.Data.SqlClient.SqlDataAdapter("select * from visitor_details", Program.con);
DataSet ds;
ds = new DataSet();
Program.adpt.Fill(ds);
if(ds.Tables[0].Rows.Cont > 0)
{
  dataGridView1.AutoGenerateColumns = true;
  dataGridView1.DataSource = ds.Tables[0];
}
 
Share this answer
 
Better before start working have some knowledge on binding the DataGridView How to populate DataGridView, GridView with SQL statement in C#[^]

Program.adpt = new System.Data.SqlClient.SqlDataAdapter("select * from visitor_details", Program.con);
DataSet ds;
ds = new DataSet();
Program.adpt.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource =ds;
 
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