C# winform DatagridView 的簡單操作

來源:互聯網
上載者:User

標籤:

資料顯示操作:

dgBill.Columns[0].DataPropertyName = "key1";
dgBill.Columns[1].DataPropertyName = "key2";
dgBill.Columns[2].DataPropertyName = "key3";

DataTable dt = new DataTable();
dt.Columns.Add("key1", Type.GetType("System.String"));
dt.Columns.Add("key2", Type.GetType("System.String"));
dt.Columns.Add("key3", Type.GetType("System.String"));

DataRow dr = null;

dr = dt.NewRow();

dr["key1"] = "key1";
dr["key1"] =  "key2";
dr["key1"] = "key3";
dt.Rows.Add(dr);
this.dgBill.DataSource = dt;

選擇一行資料:

//右鍵菜單事項
private void dg1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
//若行已是選中狀態就不再進行設定
dg1.ClearSelection();
dg1.Rows[e.RowIndex].Selected = true;

//設定當前行
dg1.CurrentCell = dg1.Rows[e.RowIndex].Cells[0];

//彈出操作菜單
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}

//選擇菜單的時候,進行擷取右鍵一行的資料

DataGridViewRow rowData = dgBill.CurrentRow;
if (rowData != null)
{
if (rowData.Cells["ID"].Value != null)
{

}
else
{
MessageBox.Show("資料轉換異常", "提示", MessageBoxButtons.OK);
}
}

C# winform DatagridView 的簡單操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.