无法在Gridview中以编程方式添加行。索引错误

时间:2017-09-12 05:27:37

标签: c# mysql

您好我想从数据库检索数据到Gridview。我正在使用自定义网格这是c#代码

try
{
    int i=0;
    MySqlCommand cmd = new MySqlCommand("select accounts,credit,debit,sum(credit) as c,sum(debit) as d from tbl_open_balance", con);
    dr=cmd.ExecuteReader();
    while(dr.Read())
    {
        dataGridView1.Rows[i].Cells[0].Value=dr["accounts"].ToString();
        dataGridView1.Rows[i].Cells[1].Value = dr["credit"].ToString();
        dataGridView1.Rows[i].Cells[2].Value = dr["debit"].ToString();
        dataGridView1.Rows.Add();
        i++;
    }
    dataGridView1.BorderStyle = BorderStyle.None;
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(238,239,249);
    dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.DarkTurquoise;
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.WhiteSmoke;
    dataGridView1.BackgroundColor = Color.White;

    dataGridView1.EnableHeadersVisualStyles = false;
    dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(20, 25, 72);
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
    dr.Close();

}
catch (Exception ex)
{
    dr.Close();
}

我收到错误索引超出范围。必须是非负数且小于集合的大小。 参数名称:index

2 个答案:

答案 0 :(得分:0)

我支持可汗提议。尝试从字符串数组中的datareader中捕获一行,并使用Datagridview.Rows.Add(myArray)添加;到网格。因此,您将其留在网格中以获取并将值分配给下一行。

答案 1 :(得分:0)

我找到了答案。如果不创建行,我想先检查是否先创建了行。这是代码

 try
        {
            int i=0;
            double credit=0.00, debit=0.00;
            MySqlCommand cmd = new MySqlCommand("select accounts,credit,debit from tbl_open_balance where status='A'", con);
            dr=cmd.ExecuteReader();
            while(dr.Read())
            {
                if (dataGridView1.Rows.Count < i + 1) dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value=dr["accounts"].ToString();
                dataGridView1.Rows[i].Cells[1].Value = dr["credit"].ToString();
                credit = credit + Convert.ToDouble(dr["credit"].ToString());
                dataGridView1.Rows[i].Cells[2].Value = dr["debit"].ToString();
                debit = debit + Convert.ToDouble(dr["debit"].ToString());
                dataGridView1.Rows.Add();
                i++;
            }
            dataGridView1.Rows[i].Cells[0].Value = "Credit";
            dataGridView1.Rows[i].Cells[1].Value = "";
            dataGridView1.Rows[i].Cells[2].Value = credit.ToString();
            dataGridView1.Rows.Add();
            i++;
            dataGridView1.Rows[i].Cells[0].Value = "Debit";
            dataGridView1.Rows[i].Cells[1].Value = "";
            dataGridView1.Rows[i].Cells[2].Value = debit.ToString();
            dataGridView1.Rows.Add();
            i++;
            dataGridView1.Rows[i].Cells[0].Value = "Total";
            dataGridView1.Rows[i].Cells[1].Value = "";
            dataGridView1.Rows[i].Cells[2].Value = (credit - debit).ToString();
            dataGridView1.Rows.Add();
            dataGridView1.BorderStyle = BorderStyle.None;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(238,239,249);
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.DarkTurquoise;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.WhiteSmoke;
            dataGridView1.BackgroundColor = Color.White;

            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(20, 25, 72);
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dr.Close();

        }
        catch (Exception ex)
        {
            dr.Close();
        }