简体   繁体   English

从 C# 中的组合框项设置 typeof 值

[英]Setting the typeof value from combobox Items in C#

I am new to C# and I am making a simple table by coding not connecting to an external database.我是 C# 的新手,我正在通过编码而不连接到外部数据库来制作一个简单的表。 I have a Combobox (Dropdown List) so that the user can select the data type of the Column from dropdown list items and then that selected Item is set as the data type of the Column.我有一个组合框(下拉列表),以便用户可以从下拉列表项中选择列的数据类型,然后将所选项目设置为列的数据类型。 I need the selected item in this line :我需要这一行中的选定项目:

            customer.Columns.Add(cn , typeof(long));

and this is my whole class :这是我的整个班级:

 namespace HomeWork1
 {
 public partial class Form1 : Form
 {
    public String tn;
    public String cn;
    DataTable customer;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("Boolean");
        comboBox1.Items.Add("Byte");
        comboBox1.Items.Add("Char");
        comboBox1.Items.Add("DateTime");
        comboBox1.Items.Add("Decimal");
        comboBox1.Items.Add("Double");
        comboBox1.Items.Add("Int16");
        comboBox1.Items.Add("Int32");
        comboBox1.Items.Add("Int64");
        comboBox1.Items.Add("String");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tn = txtTableName.Text;

        customer = new DataTable(tn);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        cn = txtColumnName.Text;
        customer.Columns.Add(cn , typeof(long));
        dataGridView1.DataSource = customer;
    }
 }
 }

C# is case sensitive. C# 区分大小写。 combobox1 and comboBox1 are not the same. combobox1comboBox1是不一样的。 Since your combo box is named comboBox1 with an upper case "B", write it like this:由于您的组合框被命名为带有大写“B”的comboBox1 ,请这样写:

customer.Columns.Add(cn ,
    Type.GetType("System." + (string)comboBox1.SelectedItem));
'                                         ^ upper case "B"

You can have two variables which differ only in upper / lower case:您可以有两个仅在大写/小写上不同的变量:

int x = 1;
int X = 100;

These are really two different variables!这真的是两个不同的变量!


Note: there are different ways of getting a Type object:注意:有多种获取Type对象的方法:

  1. From a type name given as identifier从作为标识符给出的类型名称

     Type t = typeof(int); // typeof(int) is known at compile time.
  2. From an object从一个对象

     Type t = someObject.GetType(); // Known only at runtime.
  3. From a type name given as string从以字符串形式给出的类型名称

     string s = "System.Int32"; Type t = Type.GetType(s);

Note: GetType() is a method all types inherit from System.Object .注意: GetType()是所有类型都继承自System.Object


Since you have type name strings in your combo box, use the third version.由于您的组合框中有类型名称字符串,因此请使用第三个版本。

Type t = Type.GetType("System." + (string)comboBox1.SelectedItem);
customer.Columns.Add(cn , t);

Or directly add Type objects to your combo box instead of strings:或者直接将Type对象添加到组合框而不是字符串:

comboBox1.Items.Add(typeof(bool));
comboBox1.Items.Add(typeof(byte));

Then simply get the type with然后简单地获取类型

Type t = (Type)comboBox1.SelectedItem;
customer.Columns.Add(cn , t);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM