C#關於DataGridView控件的使用總結
【請按照換行進行對應代碼段】——Frank_chen
DataGridView常用方法:
dataGridView1.ReadOnly = true;//全部單元格只讀
dataGridView1.Columns[1].ReadOnly = true;///指定單元格設置只讀(列)第一列
dataGridView1.Rows[2].ReadOnly = true;///指定單元格設置只讀 (行)第二行
dataGridView1[1, 2].ReadOnly = true;///指定單元格設置只讀 (第1行,第2列)利用座標
dataGridView1.AllowUserToAddRows = false;//禁止用戶追加行
dataGridView1.AllowUserToDeleteRows = true;//允許用戶刪除行操作
//提示是否刪除指定行數據
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
DialogResult diaR = MessageBox.Show("刪除該行嗎?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (diaR == DialogResult.OK)
{
e.Cancel = false;
}
}
//提示刪除了哪一行數據
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "行號爲", e.Row);
messageBoxCS.AppendLine();
DialogResult diaR = MessageBox.Show("刪除了" + messageBoxCS.ToString(), "確認");
}
//刪除選定的多行
foreach( DataGridViewRow r in dataGridView1.SelectedRows)
{ if (r.IsNewRow == false) { ataGridView1.Rows.Remove(r); }
}
取得選定的行、列、單元格
//選定的單元格
foreach (DataGridViewCell c in dataGridView1.SelectedCells)
{
string cr = string.Format("{0},{1}", c.ColumnIndex, c.RowIndex);
listBox1.Items.Add("選定的單元格位置是:" + cr);
}
//選定的行/列
foreach (DataGridViewRow c in dataGridView1.SelectedRows)
{
listBox1.Items.Add("選定的行是:" + c.RowIndex);
}
foreach (DataGridViewColumn c in dataGridView1.SelectedColumns)
{
listBox1.Items.Add("選定的列是:" + c.ColumnIndex);
}
//指定選定單元格
dataGridView1[0, 0].Selected = true;
dataGridView1.Rows[0].Selected = true;
dataGridView1.Columns[0].Selected = true;
//設置行內容
dataGridView1.Rows[0].HeaderCell.Value = "第1行";
//利用數組添加添加數據時,類型都是默認的字符串類型
string name = "Jim";
string gender = "男";
string age = "18";
//將上面三個變量合成一個數組
string[] row = { name, gender, age };
//給dataGridView1控件添加數據
dataGridView1.Rows.Add(row);//添加數據
/
/左上角的文字
dataGridView1.TopLeftHeaderCell.Value = "左上角";
//列中顯示選擇框CheckBox
DataGridViewCheckBoxColumn column1= new DataGridViewCheckBoxColumn();
{
column1.HeaderText = "選擇框";
column1.Name = "checkbox";
column1.AutoSizeMode =
DataGridViewAutoSizeColumnMode.DisplayedCells;
column1.FlatStyle = FlatStyle.Standard;
//顯示選擇框的三種狀態
column1.ThreeState = true;
}
dataGridView1.Columns.Add(column1);
//顯示按鈕控件
DataGridViewButtonColumn col=new DataGridViewButtonColumn();
col.Name="Button";
col.UseColumnTextForButtonValue=true;
col.Text = "按鈕";
dataGridView1.Columns.Add(col);
觸發的事件爲:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name=="Button")
{
MessageBox.Show("觸發了按鈕");
}
}
單元格列顯示圖像的是:DataGridViewImageColumn
//顯示圖像
DataGridViewImageColumn dgvI=new DataGridViewImageColumn();
dgvI.Name="Image";
dgvI.ValuesAreIcons=false;
dgvI.Image = new Bitmap("c://windows//Blue Lace 16.bmp");
dgvI.ImageLayout=DataGridViewImageCellLayout.Zoom;
dgvI.Description="測試的圖片";
dataGridView1.Columns.Add(dgvI);
dataGridView1["Image", 0].Value = new Bitmap("c://windows//Blue Lace 16.bmp");
//獲取總行數:
dataGridView1.Rows.Count;
獲取當前選中行索引:
int i = this.dataGridView1.CurrentRow.Index;
獲取當前選中列索引:
int j = this.dataGridView1.CurrentCell.ColumnIndex;