[英]Datagridview add row with cell style programmatically
dgvStatus
是一個包含一列的DataGridView。
以下行是添加新行
dgvStatus.Rows.Add("XYZ");
但我想改變單元格文本顏色,所以我寫了下面的代碼
DataGridViewRow row = new DataGridViewRow();
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.ForeColor = Color.Red; // the color change
row.DefaultCellStyle = style;
row.Cells[0].Value = "XYZ";
dgvStatus.Rows.Add(row);
但是這段代碼給出了錯誤 -
如何解決它。
更新:
當我根據@ASh
的答案改變我的代碼
dgvStatus.Rows.Add(row);
row.Cells[0].Value = "XYZ";
然后它給出以下錯誤 -
在將行添加到網格之前,行沒有單元格
dgvStatus.Rows.Add(row);
row.Cells[0].Value = "XYZ";
UPDATE
如果它不起作用,試試這個:
int idx = dgvStatus.Rows.Add("test");
var row = dgvStatus.Rows[idx];
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.ForeColor = Color.Red; // the color change
row.DefaultCellStyle = style;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.