什么是datagridviewcell和datagridviewcolumn之间的差异?

时间:2018-02-17 06:28:43

标签: winforms datagridview

datagridviewcelldatagridviewcolumn之间的区别是什么?

我目前有这个代码来设置我的对象编辑器,但我无法弄清楚为什么datagridviewcolumns没有值属性而行没有添加属性?

public partial class ObjectDialog : Form
{
    string DefaultEntryName = "Object";

    public ObjectDialog()
    {
        InitializeComponent();

        DataGridViewRow           row  = new DataGridViewRow();
        DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();

        col1.Visible      = true;
        col1.ReadOnly     = true;
        col1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        //col1.Value        = ""; Not a Valid Property
        //row.Add(col1)           Not a Valid Property

        col2.Visible      = true;
        col2.ReadOnly     = true;
        col2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        //col2.Value        = ""; Not a Valid Property
        //row.Add(col2)           Not a Valid Property

        col3.Visible      = false;
        col3.ReadOnly     = true;
        col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
        //col3.Value        = ""; Not a Valid Property
        //row.Add(col3)           Not a Valid Property
        dataGridView.Rows.Add(row);
    }

1 个答案:

答案 0 :(得分:0)

好的,所以我有这个工作.. 这就是我现在添加新行的方式,因为我知道我的行,列和单元格是什么。

    public ObjectDialog()
    {
        InitializeComponent();

        int col1Id = dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
        int col2Id = dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
        int col3Id = dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
        int rowId  = dataGridView.Rows.Add();


        //Column 1
        DataGridViewTextBoxColumn col1 = (DataGridViewTextBoxColumn)dataGridView.Columns[col1Id];
        col1.Visible = true;
        col1.ReadOnly = true;
        col1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;


        //Column 2
        DataGridViewTextBoxColumn col2 = (DataGridViewTextBoxColumn)dataGridView.Columns[col2Id];
        col2.Visible = true;
        col2.ReadOnly = true;
        col2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


        //Column 3
        DataGridViewTextBoxColumn col3 = (DataGridViewTextBoxColumn)dataGridView.Columns[col3Id];
        col3.Visible = false;
        col3.ReadOnly = true;
        col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

        //Row
        DataGridViewRow row = dataGridView.Rows[rowId];
        row.Cells[0].Value = "0";
        row.Cells[1].Value = DefaultEntryName + " 0";
        row.Cells[2].Value = "";
    }