Thursday, October 29, 2009

Adding ComboBox to datagrid view in windows

 dataGridView1 .AutoGenerateColumns =false ;

           SqlConnection con = new SqlConnection("User Id=sa; Password=server;Database=vamsi");
           SqlDataAdapter da = new SqlDataAdapter("Select * from details", con);
           DataSet ds = new DataSet();
           da.Fill(ds, "details");
           dataGridView1.DataSource = ds.Tables[0];
           DataGridViewColumn column1 = new DataGridViewComboBoxColumn();
            column1 .DataPropertyName ="No";
            column1 .Name ="Number";
            column1.ReadOnly = true;
            dataGridView1 .Columns.Add (column1);
            DataGridViewColumn column2 =new DataGridViewTextBoxColumn();
            column2.DataPropertyName ="Name";
            column2.Name ="Name";
            dataGridView1 .Columns.Add (column2);
            DataGridViewColumn column3 =new DataGridViewTextBoxColumn();
            column3.DataPropertyName ="Age";
            column3.Name ="Age";
            dataGridView1.Columns.Add (column3);
            DataGridViewColumn column4 =new DataGridViewTextBoxColumn();
            column4.DataPropertyName ="City";
            column4.Name ="City";
            dataGridView1 .Columns.Add (column4);
            this .Controls.Add (dataGridView1);

No comments:

Post a Comment