views:

31

answers:

1

Hi Guys.

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.

A: 

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();
        }
MAW74656