Datagridview如何新增一個序號欄位(透過Dataset)?

1. 填入(通常從資料庫)Dataset的值後,先新增mySeq欄到此Dataset :
if (!myDataset.Tables["myTable"].Columns.Contains("mySeq"))
    myDataset.Tables["myTable"].Columns.Add("mySeq", typeof(int));

2. Datagridview指定DataSource DataMember :
dataGridView1.DataSource = myDataset;
dataGridView1.DataMember = "myTable";

3. DatagridviewRowPostPaint Event 新增以下 :
private voiddataGridView1_RowPostPaint(object sender,DataGridViewRowPostPaintEventArgs e)
{
    if((dataGridView1.Rows[e.RowIndex].Cells["mySeq"].Value).ToString() == "")
        dataGridView1.Rows[e.RowIndex].Cells["mySeq"].Value = e.RowIndex + 1;
    else
        return;
}

Read More »
>