0

Hello sir,
I have an application in which on button click there will be pop up window and on that window i have to show data from database in tabular format So. how i can do this

3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
0

Create a new form for your pop-up window. Put a DataGridView on it. Bind the query to the DataGridView. Done.

0

Tabular? Whats is this? Does the dataGridView control satisify your expectations?
So, back to your issue.
Get wanted values from database( use DataTable to fill it up), then open Form2 (new pop-up window) and pass dataTable parameter to it (it can be passed in the constructor of form2). Then bind dataGridView with dataTable (use dataSource). And here you go . data from database are in pop-up window.

//in rough:

//On form1:
        private void OpenPopUp()
        {
            DataTable table = GetData();
            if (table.Rows.Count > 0)
            {
                Form2 f2 = new Form2(table);
                f2.Show(this);
            }
        }

        private DataTable GetData()
        {
            using (SqlConnection sqlConn = new SqlConnection("ConnectionString"))
            {
                string query = "SELECT a, b, c FROM MyTable";
                SqlDataAdapter da = new SqlDataAdapter(query, sqlConn);
                DataTable table = new DataTable("myTable");
                da.SelectCommand.Connection = sqlConn;
                da.SelectCommand.Connection.Open();
                da.Fill(table);
                return table;
            }
        }


//on form2 (pop-up):
    public partial class Form2 : Form
    {
        public Form2 (DataTable table)
        {
            DataGridView dgv = new DataGridView();
            dgv.Location = new Point(10, 10);
            dgv.AllowUserToAddRows = false;
            dgv.RowHeadersVisible = false;
            dgv.ReadOnly = true;
            this.Controls.Add(dgv);
            dgv.DataSource = new BindingSource(table, null);
        }
    }

If this is no clear enough, let me know.
Mitja

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.