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

问题描述

      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


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;
        }


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

10-27 12:58