开发者

Binding multiple textbox controls to database rows

开发者 https://www.devze.com 2023-01-18 13:16 出处:网络
I´m trying to store some parameters for my application on a database table. The table contains multiple rows which contains a Key -> Value relationships. On my windows forms program (written in C#) I

I´m trying to store some parameters for my application on a database table. The table contains multiple rows which contains a Key -> Value relationships. On my windows forms program (written in C#) I have multiple controls that should allow the user to interact with the stored configuration on the database.

For example, my开发者_运维百科 database table looks like the following:

id option_name               option_value
1  serial_port_baudrate      9600
2  serial_port_stopbits      one

On my form I have controls that allow to change those values. After some research I've obtained the expected results using the bindingSource class and the filter property. But I have to create a bindingSource object for each textbox using the visual editor. Can you please help me to archieve this with multiple textboxes and controls without using the visual editor?

I dont know if it´s possible but I would like to code a solution where I could just name the textboxes on my form with the same name of the Key that holds the value in the database and then they would get the correct values when the application runs. I would appreciate any information or hints you could provide.

Thanks in advance.


Well, you could name all the text boxes the same as the database fields, then loop through all the textbox controls on the form. Use the name of the form as a parameter as in the below code:

 DataTable table = new DataTable();

            //Populate datatable
    TextBox txt = new TextBox();
          foreach(DataRow row in table.Rows)
        {
            String controlID = row["option_name"].ToString();


              txt.ID = controlID;

              (new TextBox()).ID = controlID;

              txt.Text = table.Rows[table.Rows.IndexOf(row)][controlID].ToString();
        }


This is what I use for my Options table:

    void loadOptions(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            DataRow[] res = options.Select("option='" + c.Name + "'");
            if (c is GroupBox)
            {
                loadOptions(c);
                continue;
            }
            if (res.Length < 1) continue;
            DataRow row = (DataRow)res.GetValue(0);
            String value = row["value"].ToString();
            if (c is TextBox)
            {
                c.Text = value;
            }
            else if (c is CheckBox)
            {
                ((CheckBox)c).Checked = Convert.ToBoolean(value);
            }
            else if (c is NumericUpDown)
            {
                ((NumericUpDown)c).Value = int.Parse(value);
            }
        }
    }

I'm not sure if this is the best solution, but it works. I'm always looking for a better way if anybody has one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号