Hunar
Hunar

Reputation: 399

make first column of datagridview auto generate number?

i have a windows form in c# on this form i have a datagridview named datagridview1 and i use this code to bind datagridview1 from a table 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;`

i don't have a problem with bind the datagridview... Know when i bind the datagridview it beggin binding from the first index, my question is i want to make binding beggin from the second index to make the first index an auto generate number?

Upvotes: 0

Views: 2532

Answers (2)

Deepak
Deepak

Reputation: 624

Maybe it's better to make row's header to display auto-incremented value?

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");

  }
}

Upvotes: 3

Dmitry
Dmitry

Reputation: 14059

Maybe it's better to make row's header to display auto-incremented value?

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

Upvotes: 0

Related Questions