在我的VS2015 Winform应用程序中,有一个DataGridView控件绑定到一个绑定到SQL数据库的BindingSource。网格有四列: ID、URL、Name、Type。URL列是DataGridViewLinkColumn,其ReadOnly属性在默认情况下设置为False。我可以编辑“名称”和“类型”列,但URL列显示为ReadOnly。为什么?如何使URL列可编辑?
发布于 2016-03-19 02:39:18
正如Reza所说:
DataGridViewLinkColumn不可编辑。
因此,要编辑此类列中的单元格,必须根据需要将其转换为DataGridViewTextBoxCell。例如,如果我订阅了DataGridView.CellContentClick来处理单击链接,那么我将处理单元格转换的CellDoubleClick:
private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"])
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = new DataGridViewTextBoxCell();
this.dataGridView1.BeginEdit(true);
}
}输入值并离开单元格后,在将单元格转换回DataGridViewLinkCell之前,应使用CellValidated验证新值是否为URI
private void DataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] == this.dataGridView1.Columns["URL"])
{
DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (Uri.IsWellFormedUriString(cell.EditedFormattedValue.ToString(), UriKind.Absolute))
{
cell = new DataGridViewLinkCell();
}
}
}警告:
DataGridViewTextBoxColumn -强制手动转换以链接单元格:private void DataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e) { foreach (DataGridViewRow r in dataGridView1.Row){ if (Uri.IsWellFormedUriString(r.Cells"URL".Value.ToString(),UriKind.Absolute){ r.Cells"URL“=新的DataGridViewLinkCell();}
DataGridViewLinkColumn,允许成功将单元格转换为TextBox类型。但在转换回链接单元格时,调试显示发生了转换,但单元格格式和行为失败。https://stackoverflow.com/questions/36073323
复制相似问题