如何解决输入数组的长度比列数多 [英] How to solve input array is longer than the number of columns

查看:80
本文介绍了如何解决输入数组的长度比列数多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      protected void Add_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
    //createnewrow();
    ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";

    GridView1.DataSource = ds;
    GridView1.DataBind();
    GridView1.DataSource = null;

}





我的尝试:



我尝试将数据添加到表中。它显示错误消息输入数组长于列数



What I have tried:

I try to add data to the table. It display an error message Input array is longer than the number of columns

推荐答案

引用:

输入数组长于列数

您正在尝试向dataRow添加更多列值,

You are trying to add more number of column values to the dataRow,

ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);



检查列数并添加正确的列值数


check the columns count and add correct number of column values

int colsCount =  dt.Columns.Count;





参考这个例子:



refer this example:

DataTable dt = new DataTable();
       dt.Columns.Add("Col1");
       dt.Rows.Add("Col1 Data"); // Valid
       dt.Rows.Add("Col1 Data", "Col2 Data"); // invalid ,  Input array is longer than the number of columns


您好,您正在尝试添加一个包含3列的新行数据表ds中有零(0)列。解决方案:向datatable添加列。

注意:如果在gridview1中添加了任何数据绑定列,请确保数据集/数据表ds的列名与gridview的DataField中的名称相同数据绑定列。



试用此代码:



Hi, you are trying to add a new row which has 3 columns while you have zero(0) columns in your datatable ds. Solution: add columns to datatable.
Note: if you have added any databound column in your gridview1, make sure the column names of your dataset/datatable ds have the same names as in the DataField of your gridview Databound columns.

Try this code:

protected void Add_Click(object sender, EventArgs e)
        {
            DataTable ds = new DataTable();

            ds.Columns.Add(new DataColumn("Col1", typeof(string)));
            ds.Columns.Add(new DataColumn("Col2", typeof(string)));
            ds.Columns.Add(new DataColumn("Col3", typeof(string)));
            GridView1.Visible = true;
            //createnewrow();
            
            ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";

            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.DataSource = null;
        }


这篇关于如何解决输入数组的长度比列数多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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