DataGridView.DataSource属性未将ColumnCount分配给DGV。 [英] DataGridView.DataSource property does not assign ColumnCount to DGV.

查看:60
本文介绍了DataGridView.DataSource属性未将ColumnCount分配给DGV。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将DataTable分配给DataGridView.DataSource。当我尝试使用新的标题文本和宽度自定义DGV列时,将为所提供的列索引抛出索引超出范围的异常。我调试以检查DataTable是否不包含列。但是,该表填充了所需的4列。然后,我在分配数据源后继续检查DGV的列数。这是0.



前一天工作正常的前一天。今天,相同的代码不起作用。

以下是代码片段:



I have assigned a DataTable to DataGridView.DataSource. When I try to customize DGV columns with new header text and width, an Index out of range exception is thrown for the column index provided. I debug to check if DataTable contains no columns. But, the table was populated with desired 4 columns. Then, I went on checking for column count of DGV after datasource was assigned. It was 0.

A day before everything was working fine. Today, same code is not working.
Here is the code snippet:

private void FormPlay_Load(object sender, EventArgs e)
        {
            DGVCreate();
            FillDGV();
            this.Controls.Add(_dgv);
        }










void DGVCreate()
        {
            _dgv = new DataGridView();
            _dgv.Location = new Point(5, 20);
            _dgv.AutoSize = true;
            _dgv.ForeColor = Color.Black;            
            _dgv.CellContentClick += _dgv_CellContentClick;
        }







void FillDGV()
       {

           _dataSet = ClassDBOperations.DatabaseAdapter("select CCode,CName,Gender,Address from ContestantProfile", "ContestantProfile");
           try
           {
               _tbl = _dataSet.Tables[0];
               if (_tbl.Rows.Count != 0)
               {
                   _dgv.DataSource = _tbl;
                   _dgv.Columns[0].HeaderText = "Code";
                   _dgv.Columns[1].HeaderText = "Name";
                   _dgv.Columns[2].HeaderText = "Gender";
                   _dgv.Columns[3].HeaderText = "Address";

                   _dgv.Columns[0].Width = 100;
                   _dgv.Columns[1].Width = 200;
                   _dgv.Columns[2].Width = 50;
                   _dgv.Columns[3].Width = 400;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }

推荐答案

首先添加列,最后设置数据源

add the columns first and finally set the data source
dataSet = ClassDBOperations.DatabaseAdapter("select CCode,CName,Gender,Address from ContestantProfile", "ContestantProfile");
dgv.AutoGenerateColumns = false;
_dgv.ColumnCount = 4;
_dgv.Columns[0].Width  = 100;
_dgv.Columns[0].HeaderText = "Code";
_dgv.Columns[0].DataPropertyName = "CCode";

_dgv.Columns[1].Width = 200;
_dgv.Columns[1].HeaderText = "Name";
_dgv.Columns[1].DataPropertyName = "CName";

_dgv.Columns[2].Width = 50;
_dgv.Columns[2].HeaderText = "Gender";
_dgv.Columns[2].DataPropertyName = "Gender";

_dgv.Columns[3].Width = 400;
_dgv.Columns[3].HeaderText = "Address ";
_dgv.Columns[3].DataPropertyName = "Address ";

_dgv.DataSource = _dataSet.Tables[0];


这篇关于DataGridView.DataSource属性未将ColumnCount分配给DGV。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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