none
radio buttons in winforms

    Question

  •  

    I am trying to bind data from database to radio buttons in a winform using C#.  e.g If you have N databases in a server ,I should be able to read the N number of databases from the server which is a dynamic data and should be able to display all of them in a winform with each radio button being assigned to the name of the database read from the server.When I click on the name of the database displayed using radio button i should be taken to a form which displays all the tables in that database again using radio buttons.

    How do I implement this?

    thanks

    Tuesday, November 27, 2007 1:46 AM

Answers

  • Hi, Anonymous,

     

    Based on my understanding, you want to bind the DataBase information to a list of RadioButtons and show the related Tables when click the RadioButtons, don't you?

     

    I think you can do your job in this way.

    First, add a Panel on your main form for adding the RadioButtons.

    You can try these codes.

    Code Block

            private void Form1_Load(object sender, EventArgs e)

            {

                SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=True");

                SqlCommand comm = new SqlCommand();

                comm.CommandText = "sp_databases"; //This procedure can be used to show all DataBases

                comm.Connection = conn;

                comm.CommandType = CommandType.StoredProcedure;

     

                int height = 0;

     

                try

                {

                    conn.Open();

                    SqlDataReader reader = comm.ExecuteReader();

                    while (reader.Read())

                    {

                        RadioButton button = new RadioButton(); //Add RadioButtons to the Panel

                        button.Text = reader[0].ToString();

                        button.Click += new EventHandler(button_Click);

                        button.Top = height;

                        panel1.Controls.Add(button);

                        height += button.Height;

     

                    }

                    reader.Close();

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                   

                }

                finally

                {

                    conn.Close();

                }

     

     

            }

     

            void button_Click(object sender, EventArgs e)

            {

                Form2 form = new Form2((sender as RadioButton).Text); //Show the Form containing the Tables

                form.Show();

            }

     

     

    As you see, we need a Form2 to show the tables.

    Add a new form to your project and add a ListBox onto it.

    This ListBox will be used to show the tables.

    For example

    Code Block

            string connectionstr;

            public Form2(string DataBaseName)

            {

                InitializeComponent();

                connectionstr = "Data Source=localhost;Integrated Security=True;Initial Catalog=" + DataBaseName + ";";

                listBox1.DisplayMember = "TABLE_NAME";

                listBox1.ValueMember = "TABLE_NAME";

               

            }

     

            private void Form2_Load(object sender, EventArgs e)

            {

                SqlConnection conn = new SqlConnection(connectionstr);

                try

                {

                    conn.Open();

                    DataTable schemaTable = conn.GetSchema("Tables"); //This will select all the Tables from the DataBase

                    listBox1.DataSource = schemaTable;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

                finally

                {

                    conn.Close();

                }

            }

     

     

    Hope this helps,

    Regards

    Friday, November 30, 2007 5:11 AM