Windows Develop Bookmark and Share   
 index > Windows Forms General > Setting DataGridView column type for late binding
 

Setting DataGridView column type for late binding

Hello, In the designer I can set a DataGridViews column type from a textbox to a ComboBox. However if I do LATE binding I can not figure out how to do this.... for example if I have the code

DataTable table;
        public Form1()
        {
            
            InitializeComponent();
            DataTable table = GetTable();
            dataGridView1.DataSource = table;
        }

        static DataTable GetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            //
            // Here we add five DataRows.
            //
            table.Rows.Add(25, "AKA", "Artic Kansas Assoc.", DateTime.Now);
            table.Rows.Add(50, "AAF", "American Association", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
            return table;
        }

    }
Then the datagridview will display all columns as textBox Columns.... I want to make one column a specific type (ie like a combobox column).
CommanderKeen  Wednesday, September 09, 2009 4:30 PM

You will have set autogeneratecolumn property as false and generate columns by code as shown in below code

            DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
            c.Name="clmColumn1";
            c.HeaderText = "Dosage";
            c.DataPropertyName = "Dosage";
            dataGridView1.Columns.Add(c);

            DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
            t.Name="clmColumn2";
            c.HeaderText = "Drug";
            t.DataPropertyName = "Drug";
            dataGridView1.Columns.Add(t);

            dataGridView1.DataSource = GetTable();



Gaurav Khanna
  • Marked As Answer byCommanderKeen Wednesday, September 09, 2009 6:50 PM
  •  
Khanna Gaurav  Wednesday, September 09, 2009 6:38 PM

You will have set autogeneratecolumn property as false and generate columns by code as shown in below code

            DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
            c.Name="clmColumn1";
            c.HeaderText = "Dosage";
            c.DataPropertyName = "Dosage";
            dataGridView1.Columns.Add(c);

            DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
            t.Name="clmColumn2";
            c.HeaderText = "Drug";
            t.DataPropertyName = "Drug";
            dataGridView1.Columns.Add(t);

            dataGridView1.DataSource = GetTable();



Gaurav Khanna
  • Marked As Answer byCommanderKeen Wednesday, September 09, 2009 6:50 PM
  •  
Khanna Gaurav  Wednesday, September 09, 2009 6:38 PM
Awesome thanks!
CommanderKeen  Wednesday, September 09, 2009 6:51 PM
=P^)
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, September 09, 2009 7:14 PM

You can use google to search for other answers

Custom Search

More Threads

• security problem in Windows applications using c#.net
• How to process keydown event while dodragdrop
• Windows OS for good performance of sql server
• Rolling logs with log4net?
• Add ins
• Ascii File Save Problems
• Autoredraw, Paint, etc.
• How do i draw lines (sections) in a design application
• Cannot create a child list for field
• in arrays...