简体   繁体   中英

How initialize dataset table that is not in a database?

I created a table in dataset that is not in a database, I need to set data to this table and bind data to a combobox, how can I do this?

        DataRowView drv = statusBindingSource.AddNew() as DataRowView;
        drv.Row["Value"] = 1;
        drv.Row["Name"] = "Active";
        statusBindingSource.EndEdit();

        statusBindingSource.Add(drv);

I cannot add external objects to this list.

The new DataTable is created, filled with data and bound to the combo box. I did not include a DataSet or BindingSource.

    private void Form1_Load(object sender, EventArgs e)
    {
        CreateDataTable();
        cbo1.SelectedIndex = -1;
    }

    private void CreateDataTable()
    {
        //Create new DataTable
        DataTable dt = new DataTable();
        //Add colums with column name and datatype
        dt.Columns.Add("Value", Type.GetType("System.Int32"));
        dt.Columns.Add("Name", Type.GetType("System.String"));
        //Add data
        object[] data = { 1, "Active" };
        dt.Rows.Add(data);
        object[] data2 = { 2, "Passive" };
        dt.Rows.Add(data2);
        //Bind to combo box
        cbo1.DataSource = dt;
        cbo1.DisplayMember = "Name";
        cbo1.ValueMember = "Value";
    }

    private void Cbo1_SelectionChangeCommitted(object sender, EventArgs e)
    {
        MessageBox.Show($"The display member is {((DataRowView)cbo1.SelectedItem)["Name"]}, The value member is {cbo1.SelectedValue}");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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