简体   繁体   English

以编程方式在DataGridViewCell中编辑文本

[英]Programmatically edit text in DataGridViewCell

I need to edit the text of a DataGridViewCell. 我需要编辑DataGridViewCell的文本。 To be specific, I need to change the text from -1 to "N/A" 具体来说,我需要将文本从-1更改为"N/A"

First I tried this: 首先,我尝试了这个:

dgResults.Rows[0].Cells[0].Value = "N/A";

But it complained at runtime: 但是它在运行时抱怨:

在此处输入图片说明

After that, I tried changing the datatype of the column: 之后,我尝试更改列的数据类型:

dTable.Columns[0].DataType = System.Type.GetType("System.String");  

and I get this error: 我得到这个错误:

Cannot change DataType of a column once it has data.

I have no access to the database and cannot modify it. 我无权访问数据库,无法修改它。

您可以通过处理CellFormatting事件来做到这一点。

Why are u changing datatype of datatable? 为什么要更改数据表的数据类型? Why not change column type of dgv? 为什么不更改dgv的列类型? try this: 尝试这个:

dgResults.Columns[0].ValueType = typeof(string);
dgResults.Rows[0].Cells[0].Value = "N/A";

Use appropriate column index 使用适当的列索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM