Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void loadToinletProduct(string id)
        {
            Connection objC1 = new Connection();
            SqlDataAdapter adp;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "select * from tblstudent where stuid=" + id;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = objC1.objCon;

            try
            {
                objC1.objCon.Open();
                adp = new SqlDataAdapter(cmd);
                adp.Fill(ds);
                dt = ds.Tables[0];

                //Set AutoGenerateColumns False
                dataGridView2.AutoGenerateColumns = false;

                //Set Columns Count
                //dataGridView1.Refresh();
                dataGridView2.ColumnCount = 7;

                //    //Add Columns
                //dataGridView2.Columns[0].Name = "productName";
                //dataGridView2.Columns[0].HeaderText = "Product";
                //dataGridView2.Columns[0].DataPropertyName = "productName";
                which code to display in gridview in textbox

                dataGridView2.DataSource = dt;
            }
            catch (Exception ei)
            {
                MessageBox.Show(ei.Message);
            }
            finally
            {
                objC1.objCon.Close();
            }
        }
Posted
Updated 16-Dec-14 3:07am
v2
Comments
Sinisa Hajnal 16-Dec-14 9:11am    
You don't need columnCount explicitly set. Go into designer, add columns that you need and only bind column names here (DataPropertyName)

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900