在datagridview单元格中显示文本 [英] Displaying text in datagridview cells

查看:421
本文介绍了在datagridview单元格中显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式设计我的datagridview,已阅读文档更多有以下代码

I am designing my datagridview programmatically, an having read the documentation more have the following code

            DataGridView View = new DataGridView();
            
            DataGridViewTextBoxColumn Textbox = new DataGridViewTextBoxColumn();
            ButtonColumn.Columns.Add(Textbox);
            Textbox.Name = "Text";

            DataGridViewButtonColumn Button = new DataGridViewButtonColumn();
            ButtonColumn.Columns.Add(Button);
            Button.Name = "Date";
            Button.FlatStyle = FlatStyle.Popup;

            DataGridViewRow row = (DataGridViewRow)ButtonColumn.RowTemplate.Clone();
            row.CreateCells(ButtonColumn, Textbox, Button);

            row.Cells["Text"].Value = "1";
            row.Cells["Date"].Value = "31/12/2016";

            View.Rows.Add(row);

我没有错误,但想确保我的逻辑是正确的。  上面的代码创建了列和行和按钮,但不显示任何值。  我在运行代码时得到这个

I have no errors but want to make sure my logic is right.  The above code creates the columns and row and button, but does not display any values.  I get this when I run he code

抛出异常:System.Windows.Forms.dll中的'System.ArgumentException'

Exception thrown: 'System.ArgumentException' in System.Windows.Forms.dll

不确定为什么价值没有显示?

Not sure why the values are not displaying?

感谢任何帮助/指示。

推荐答案

您正在尝试将值设置为行模板,这并不意味着保存您的值,并且克隆它不会使它成为一行......它仍然是一个模板。

You are trying to set values to the row template, which is not meant to hold your values, and cloning it doesn't make it a row...it stays a template.

此外,您似乎已经将列添加到ButtonColumn中

Also it seems you have added columns to ButtonColumn with

ButtonColumn.Columns.add(Text);

ButtonColumn.Columns.Add(Button);

ButtonColumn.Columns.Add(Button);

//顺便说一句,你不应该' t使用控制名称作为控件名称(按钮??)

//BTW you shouldn't use the control name as the name of the control (Button??)




要设置单元格的值,请使用以下内容:

To set the values of your cells use something like:

View.Rows.Add();

Views.Rows[0].Cells["Text"].Value = "1";

Views.R ows [0] .Cells [" Date"]。Value =" 31/12 / 2016";

Views.Rows[0].Cells["Date"].Value="31/12/2016";





这篇关于在datagridview单元格中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆