make datagridview的第一列自动生成数字?

时间:2014-03-25 18:38:28

标签: c#

我在这个表单上的c#中有一个windows表单我有一个名为datagridview1的datagridview,我使用这段代码从表datagridview1绑定datagridview1

ad = new OracleDataAdapter("select BORROWER_NAME,BORROWER_CODE,BOOK_CODE
                        from BORROWED_BOOK order by BORROWER_NAME", connection);
ad.Fill(ds, "BORROWED_BOOK");
DataTable dt = ds.Tables["BORROWED_BOOK"];
bs.DataSource = ds.Tables["BORROWED_BOOK"];
dv = new DataView(ds.Tables["BORROWED_BOOK"]);
this.dataGridView1.DataSource = bs;
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].HeaderText = "name";
dataGridView1.Columns[0].Width = 200;
dataGridView1.Columns[1].HeaderText = "b_code";
dataGridView1.Columns[1].Width = 100;
dataGridView1.Columns[2].HeaderText = "book_code";
dataGridView1.Columns[2].Width = 90;`

我没有绑定datagridview的问题...知道什么时候我绑定datagridview它从第一个索引开始绑定,我的问题是我想从第二个索引做出绑定beggin来做第一个索引自动生成数字?

2 个答案:

答案 0 :(得分:3)

也许最好让row的标题显示自动递增的值?

dataGridView1.Rows.Add(comboBox2.Text, textBox1.Text, textBox2.Text);

 if (dataGridView1 != null){

   for (int count = 0; (count <= (dataGridView1.Rows.Count - 2)); count++){
       dataGridView1.Rows[count].HeaderCell.Value = string.Format((count + 1).ToString(), "0");

  }
}

答案 1 :(得分:0)

也许最好让row的标题显示自动递增的值?

dataGridView1.DataBindingComplete += (o, e) =>
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
            row.HeaderCell.Value = (row.Index + 1).ToString();
    };