文档库 最新最全的文档下载
当前位置:文档库 › DataGridView控件用法

DataGridView控件用法

这个好多方法咧。
DataGridView标题栏颜色设置无效的问题

设置DataGridView.ColumnHeaderDefaultCellStyle的BackColor属性会发现没有效果。

这是因为在启动了可视样式的时候,BackColor和ForeColor的值会被忽略。要解决很简单:

datagridview1.EnableHeadersVisualStyles = false;

1,可以直接通过DataGridView的重载运算符[]直接获取,
例如>>>>>>>>
dataGridView[0][1].Value.ToString()。这里的0是列号,1是行号。
假如,你要取第一行第三列的值dataGridView[2][0].Value.ToString()就可以了。
2,根据行来获取。
例如>>>>>>>>
你想要获取当前选定行,名称叫"Name"的单元格,你可以这样,
dataGridView.SelectedRows[0].Cells["Name"].Value.ToString()
或者,你想要获取第一行名叫"Age"的单元格,你可以这样,
dataGridView.Rows[0].Cells["Age"].Value.ToString()



private void dataGridHandle_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridHandle.Rows[e.RowIndex].Cells["Dosage"].Value = Convert.ToInt32(dataGridHandle.Rows[e.RowIndex].Cells["ThisFloor"].Value) - Convert.ToInt32(dataGridHandle.Rows[e.RowIndex].Cells["LastFloor"].Value);
}

int num1=this.dataGridView1.CurrentCell.ColumnIndex;//獲取當前單元格列的索引值
int num2 = this.dataGridView1.CurrentCell.RowIndex;//獲取當前單元格行的索引值


dataGridView1.Rows[0].Cells[0].Value.ToString(); //获取第一行第一列的值
dataGridView1.CurrentRow.Cells[0].Value.ToString(); //获取选中行第一列的值


string str = this.dataGridView1.CurrentCell.Value.ToString();//獲取選中單元格的值
string strHeaderTest = this.dataGridView1.Columns[1].HeaderCell.Value.ToString();//獲取選中列的表頭



datagridview.CurrentCell.RowIndex;是当前活动的单元格的行的索引
datagridview.SelectedRows 是选中行的集合
datagridview.SelectedColumns 是选中列的集合
datagridview.SelectedCells 是选中单元格的集合
DataGridView1.CurrentRow.Index 获得包含当前单元格的行的索引
例子:int rows = dataGridView1.indexrows;//获得选种行的索引
string str = dataGridView1.rows[rows].cells[num].text;
//获取第rows行的索引为num列的值

rivate void DataGridview1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
string value = "";
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
//取得当前单元格的值
value = grd.grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//获取单元格的坐标
int i = MousePosition.X;
int j = MousePosition.Y;
}
}


private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;//获取当前行


string s = dataGridView1.Rows[rowIndex].Cells["xh"].Value.ToString(); //获取当前行xh字段的值

MessageBox.Show(s);
}

相关文档