C#DataGrid控件的整行选取和单击响应
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#DataGrid控件的单击响应和整行选取
(1)整行选取
先将DataGridview控件中SelectionMode属性设置一下,改为fullrowselection
(2) 单击响应
使用CellContentClick 和CellClick事件
在判断某一行的时候,使用e.RowIndex属性
在判断某一列的时候,使用e.ColumnIndex属性
//DataGrid 单击;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int iRow = e.RowIndex;
int iCol = e.ColumnIndex;
if (iRow < 0 || iCol < 0)
return;
int iCnt = this.dataGridView1.Rows.Count;
int iSel = this.dataGridView1.SelectedRows.Count;
//this.dataGridView1.Rows.Count == 1 && this.dataGridView1.RowValidated
string buttonText = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// 可以获得button按钮、cell格子中文字
if (buttonText == "ss")
{
string url = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string aid = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//string newImage = this.CreateImage(imageURL, aid);
//this.dataGridView1.Rows[e.RowIndex].Cells[7].Value = System.Drawing.Image.FromFile(newImage);
}
if (buttonText == "ss")
{
//string userInput = this.dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
string url = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string aid = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//Regex reg = new Regex("&re=(.+?)&");
//string re = this.RegStr(reg, url);
//Regex rego = new Regex("&sg=(.+?)$");
//string sg = this.RegStr(rego, url);
}
}
//DataGrid 单击表头;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int iRow = e.RowIndex;
int iCol = e.ColumnIndex;
if (iRow < 0 || iCol < 0)
return;
int iCnt = this.dataGridView1.Rows.Count;
int iSel = this.dataGridView1.SelectedRows.Count;
//this.dataGridView1.Rows.Count == 1 && this.dataGridView1.RowValidated
string buttonText = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// 可以获得button按钮、cell格子中文字
if (buttonText == "ss")
{
string url = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string aid = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//string newImage = this.CreateImage(imageURL, ai
d);
//this.dataGridView1.Rows[e.RowIndex].Cells[7].Value = System.Drawing.Image.FromFile(newImage);
}
if (buttonText == "ss")
{
string url = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string aid = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//Regex reg = new Regex("&re=(.+?)&");
//string re = this.RegStr(reg, url);
//Regex rego = new Regex("&sg=(.+?)$");
//string sg = this.RegStr(rego, url);
}
}