Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one form in which i run query which gives me data of 7 rows but while i running loop to print 7 rows in dgv . only one row printed and when trying to print next row above error shows at insertion statement of dgv.

here is my code for reference,

C#
private void btnsearch_Click(object sender, EventArgs e)
{
   try
   {
     DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
     DateTime todate = Convert.ToDateTime(dtpto.Text);
     string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
     string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");

    if (ckaprov.Checked == true)
    {
       cmd = new SqlCommand();
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "Proc_GetIndentBetweenDate";
       cmd.Parameters.Add(new SqlParameter("fromDate", datetimefrm));
       cmd.Parameters.Add(new SqlParameter("@toDate", datetimeto));
       cmd.Connection = con;
       table = new DataTable();
       adapter = new SqlDataAdapter(cmd);
       adapter.Fill(table);
       int count = table.Columns.Count;

    if (table.Rows.Count > 0)
    {
       foreach (DataRow rw in table.Rows)
       {
         string no = rw["Indent No"].ToString();
         string date = rw["Indent Date"].ToString();
         string frm = rw["From Store"].ToString();
         string to = rw["To Store"].ToString();
         string status = rw["Indent Status"].ToString();
         string emergency = rw["Emergency Indent"].ToString();

        for (int i = 0; i <= table.Rows.Count - 1; i++)
        {
           dataGridView1.Rows[i].Cells[1].Value = no;
           dataGridView1.Rows[i].Cells[2].Value = date;
           dataGridView1.Rows[i].Cells[3].Value = frm;
           dataGridView1.Rows[i].Cells[4].Value = to;
           dataGridView1.Rows[i].Cells[5].Value = status;
           dataGridView1.Rows[i].Cells[6].Value = emergency;
           dataGridView1.Rows[i].Cells[7].Value = "Send";
        }
         // dataGridView1.Rows.Add();
    }
  }
else { MessageBox.Show("No indents present between these dates."); }
}
else
{
MessageBox.Show("No open indents found , Please check Approved.");
}
}
catch { }
}
Posted
Updated 5-Dec-15 0:49am
v2
Comments
deepankarbhatnagar 7-Dec-15 1:03am    
Please check the rowcount of your table.

It's not the rows.
It's the cells.

C# array indexes are zero based, so it's likely that these:
C#
for (int i = 0; i <= table.Rows.Count - 1; i++)
{
   dataGridView1.Rows[i].Cells[1].Value = no;
   dataGridView1.Rows[i].Cells[2].Value = date;
   dataGridView1.Rows[i].Cells[3].Value = frm;
   dataGridView1.Rows[i].Cells[4].Value = to;
   dataGridView1.Rows[i].Cells[5].Value = status;
   dataGridView1.Rows[i].Cells[6].Value = emergency;
   dataGridView1.Rows[i].Cells[7].Value = "Send";
}
Should be these:
C#
for (int i = 0; i <= table.Rows.Count - 1; i++)
{
   dataGridView1.Rows[i].Cells[0].Value = no;
   dataGridView1.Rows[i].Cells[1].Value = date;
   dataGridView1.Rows[i].Cells[2].Value = frm;
   dataGridView1.Rows[i].Cells[3].Value = to;
   dataGridView1.Rows[i].Cells[4].Value = status;
   dataGridView1.Rows[i].Cells[5].Value = emergency;
   dataGridView1.Rows[i].Cells[6].Value = "Send";
}

[edit]
Alternatively, it could be that you just don't have enough rows in your DGV - you would have to use the Rows.Add method instead of assuming a row exists.
[/edit]
 
Share this answer
 
v2
Comments
Member 11543226 5-Dec-15 7:20am    
cell 0 is checkbox cell i cant add any data into that so i started inserting data from cell 1 but first row inserted perfectly that i want but from 2nd row it does not inserting data.
OriginalGriff 5-Dec-15 7:38am    
So it's the alternative - you don't have enough rows.
The Rows collection doesn't expand automatically when you use Rows[index] - you have to Add sufficient DataGridViewRow items to the Rows collection.
while (dataGridView1.Rows.Count <= table.Rows.Count)
{
dataGridView1.Rows.Add(new DataGridViewRow());
}
for (int i = 0; i < table.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[1].Value = i.ToString();
}
}
Why not use the DataTable as the DataSource of the DataGridView?
Either directly or via a BindingSource.
It will make your code a bit simpler.
C#
private void btnsearch_Click(object sender, EventArgs e)
{
    try
    {
        DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
        DateTime todate = Convert.ToDateTime(dtpto.Text);
        string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
        string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");
        
        if (ckaprov.Checked == true)
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Proc_GetIndentBetweenDate";
            cmd.Parameters.Add(new SqlParameter("fromDate", datetimefrm));
            cmd.Parameters.Add(new SqlParameter("@toDate", datetimeto));
            cmd.Connection = con;
            table = new DataTable();
            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(table);

            dataGridView1.DataSource = table.DefaultView;
        }
        else
        {
            MessageBox.Show("No open indents found , Please check Approved.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Also, hiding an exception is usually a bad idea, especially during debugging.
 
Share this answer
 
Comments
Member 11543226 7-Dec-15 0:54am    
I am adding first coloum as dgvchekbox and last as dgvbutton and between these binding data from table . so found this only one way to bind data.
George Jonsson 7-Dec-15 3:46am    
There are quite easy ways to do what you want, but if you are happy with your coding it's ok.
Member 11543226 7-Dec-15 1:57am    
Got the answer by adding line
int index = this.dataGridView1.Rows.Add();

for (int i = 0; i < table.Rows.Count - 1; i++)
{
for (int j = 0; j < table.Columns.Count - 1; j++)
{
dataGridView1.Rows[index].Cells[0].Value = null;
dataGridView1.Rows[index].Cells[1].Value = no;
dataGridView1.Rows[index].Cells[2].Value = date;
dataGridView1.Rows[index].Cells[3].Value = frm;
dataGridView1.Rows[index].Cells[4].Value = to;
dataGridView1.Rows[index].Cells[5].Value = status;
dataGridView1.Rows[index].Cells[6].Value = emergency;
dataGridView1.Rows[index].Cells[7].Value = "Send";
}
}

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