Click here to Skip to main content
15,882,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts...I am printing data from datagridview using dataadapter and dataset...but i m getting error like There is no row at position 2...plz help me how i have to handle this...thanks

I m getting error in this statement in the following code:

((DataTable)wddgv.DataSource).Rows[i + row];

private void DrawGridBody(Graphics g, int y_value)
            int x_value;

            for (int i = 0; i < 5; ++i)
                DataRow dr =
                         ((DataTable)wddgv.DataSource).Rows[i + row];
                x_value = 0;

                // draw a solid line
                g.DrawLine(Pens.Black, new Point(0, y_value),
                        new Point(this.Width, y_value));

                foreach (DataGridViewColumn dc in wddgv.Columns)
                    string text = dr[dc.DataPropertyName].ToString();

                    g.DrawString(text, this.Font, Brushes.Black,
                                             (float)x_value, (float)y_value + 10f);

                    x_value += dc.Width + 5;

                y_value += 40;

            row += 5;

Your best bet would be to restrict the for block to the number of rows in the data table.

for(.int i = 0; i <= wddgv.rows.count() - 1;i++)
//your code here
Share this answer
before that check condition
data row count r else
u getting same error
i think database not available any values so only comng this error

DataRow dr =
((DataTable)wddgv.DataSource).Rows[i + row];
Share this answer
Zain -Ul- Arifeen 2-Mar-13 2:20am    
dear sir the database contains valid values.but I m getting error again and again. there is no row at position 8 or 2 or 4 etc

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