C# 更改dataGridView\u单元格格式上单元格的文本
我有一个C# 更改dataGridView\u单元格格式上单元格的文本,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个Datagridview列,其值为int。我对值status有一个条件,如果(status==1)则将文本更改为活动,否则将文本更改为非活动 任何帮助都将不胜感激 private void bindListToGridView(List<Employee> list) { // DataTable dt2 = convertListToDataTable(list); // more codes here. using (dt2) {
Datagridview
列,其值为int
。我对值status有一个条件,如果(status==1)
则将文本更改为活动
,否则将文本更改为非活动
任何帮助都将不胜感激
private void bindListToGridView(List<Employee> list)
{
// DataTable dt2 = convertListToDataTable(list);
// more codes here.
using (dt2)
{
// more codes here.
dataGridView1.Columns[8].Name = "Status";
dataGridView1.Columns[8].HeaderText = "Status";
dataGridView1.Columns[8].DataPropertyName = "status";
dataGridView1.DataSource = dt2;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string columnName = dataGridView1.Columns[e.ColumnIndex].Name;
if (columnName == "Status")
{
int value = (int)dataGridView1.Rows[e.RowIndex].Cells["Status"].Value;
if (value == 1)
{
// set the cell text = Active
}
else
{
// set the cell text = Not Active
}
}
}
private void bindListToGridView(列表)
{
//DataTable dt2=convertListToDataTable(列表);
//这里有更多的代码。
使用(dt2)
{
//这里有更多的代码。
dataGridView1.Columns[8]。Name=“Status”;
dataGridView1.Columns[8]。HeaderText=“Status”;
dataGridView1.Columns[8]。DataPropertyName=“status”;
dataGridView1.DataSource=dt2;
}
}
私有void dataGridView1_CellFormatting(对象发送方,DataGridViewCellFormattingEventArgs e)
{
字符串columnName=dataGridView1.Columns[e.ColumnIndex].Name;
如果(columnName==“状态”)
{
int value=(int)dataGridView1.Rows[e.RowIndex]。单元格[“状态”]。value;
如果(值==1)
{
//将单元格文本设置为活动
}
其他的
{
//将单元格文本设置为未激活
}
}
}
为避免更改基础整数值,可以将单元格格式设置为所需文本:
if (value == 1)
{
dataGridView1.Rows[e.RowIndex].Cells["Status"].Style.Format = "Active";
}
else
{
dataGridView1.Rows[e.RowIndex].Cells["Status"].Style.Format = "Not Active";
}
或者更容易,正如LarsTech所指出的,只是等价地设置:
e.Value = "Active";
这是因为我们已经在
CellFormatting
事件处理程序中,DataGridViewCellFormattingEventArgs
的Value
属性是格式化的转换值。我想您需要e.Value=“Active”改为“代码>谢谢!”!我非常专注于避免设置cell.Value
的错误,因此我很快跳转到cell.Style.Format
,忽略了事件参数。