Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void AddProject_Load(object sender, EventArgs e)
{

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "select p.property_name, p.property_type_id, s.type_name from tb_property p INNER JOIN lk_tb_property_type s ON p.property_type_id=s.property_type_id ";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{

for (int j = 0; j < 2; j++)
{
dataGridView1.Rows.Add(new DataGridViewRow());
dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
}
}
con.Close();
Posted

1 solution

change this
C#
for (int j = 0; j < 2; j++)


to
C#
for (int j = 0; j < 3; j++)
 
Share this answer
 
v2
Comments
Member 10512838 21-Jan-14 6:15am    
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
this error comes when i write 3 instead of 2
Karthik_Mahalingam 21-Jan-14 6:24am    
see
this query will result 3 columns
p.property_name, p.property_type_id, s.type_name
Member 10512838 21-Jan-14 6:27am    
yea i know but it is giving this error....i made the changes you suggested but it is showing the error that i posted above
Karthik_Mahalingam 21-Jan-14 6:50am    
instead of these
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{

for (int j = 0; j < 2; j++)
{
dataGridView1.Rows.Add(new DataGridViewRow());
dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i][j].ToString();
}
}

try only
dataGridView1.datasource = ds.Tables[0];
Member 10512838 21-Jan-14 6:52am    
in this case it is showing this error....
Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'datasource' and no extension method 'datasource' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)

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