输入的数组长于列

时间:2019-04-15 16:30:57

标签: c#

我有一个权限表,我想将所有按钮和MenuStrip输入数据库。

但是当我尝试使用此功能时,会出现错误消息

public DataTable loadFormsItemsNameDb(string FormName)
{
    var formtype = Assembly.GetExecutingAssembly().GetTypes()
        .Where(a => a.BaseType == typeof(Form) && a.GetTypeInfo().Name == FormName)
        .FirstOrDefault();
    Form f = (Form)Activator.CreateInstance(formtype);
    DataTable formsitem = new DataTable();

    var ms = f.Controls.OfType<MenuStrip>();

    if (ms.Count() > 0)
    {            
        for (int i= 0; i< ms.ElementAt(0).Items.Count; i++)
        {
            string name = ms.ElementAt(0).Items[i].Name;
            string Text = ms.ElementAt(0).Items[i].Text;
            string mType = ms.ElementAt(0).Items[i].GetType().ToString();

            formsitem.Rows.Add(f.Name, name, Text);
        }
    }

    var BTN = f.Controls.OfType<Button>();
    if (BTN.Count() > 0)
    {
        for (int i = 0; i < BTN.Count(); i++)
        {
            string name = BTN.ElementAt(i).Name;
            string Text = BTN.ElementAt(i).Text;
            string mType = BTN.ElementAt(i).GetType().ToString();

            formsitem.Rows.Add(f.Name, name, Text, mType);
        }
    }

    return  formsitem;
}

2 个答案:

答案 0 :(得分:0)

After you create a DataTable , you need to define its structure using columns.
for eg.

DataColumn column = new DataColumn(); 
column.DataType = System.Type.GetType("System.Decimal"); 
column.AllowDBNull = false; 
column.Caption = "Price"; 
column.ColumnName = "Price"; 
column.DefaultValue = 25; 
table.Columns.Add(column); 

,然后可以在表中添加一行。在这里,您尚未指定表格的列。

答案 1 :(得分:0)

        Form f = (Form)Activator.CreateInstance(formType);
        DataTable formsItem = new DataTable();
        //************
        // Add This Columns Name To Solve Problem ( longer than column ) 
        formsItem.Columns.Add("FIFName");
        formsItem.Columns.Add("FIName");
        formsItem.Columns.Add("FIText");
        formsItem.Columns.Add("FImType");
        // *******************
        var MS = f.Controls.OfType<MenuStrip>();