DataGridView Column Template
I have a DataGridView on my Form which is being populated with database records on the click event button. How can I populate another two column template programatically at run time?
The two column template are Qty In Stock and Status This is my datagridview what looks like when populated from database on click event button...
===============================================================
FoodName FoodType Qty In Stock Status
===============================================================
Olives Starter
Soup Starter
Caprese Starter
Bruschetta Starter
Mushroom Starter
Antipasto Starter
Scallops Starter
Calamari Starter
Crab Avocado Starter
Pizza Bread Starter
===============================================================
And this is the datagridview what I want to look like populating the other two columns at the run-time
=================================================================
FoodName FoodType Qty In Stock Status
==========开发者_StackOverflow中文版=======================================================
Olives Starter 0 Allways On Stock
Soup Starter 0 Allways On Stock
Caprese Starter 0 Allways On Stock
Bruschetta Starter 0 Allways On Stock
Mushroom Starter 0 Allways On Stock
Antipasto Starter 0 Allways On Stock
Scallops Starter 0 Allways On Stock
Calamari Starter 0 Allways On Stock
Crab Avocado Starter 0 Allways On Stock
Pizza Bread Starter 0 Allways On Stock
=================================================================
Here is the code generating the datagridview from database on click event button...
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();
private void cmdStarters_Click(object sender, EventArgs e)
{
OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connBuilder.Add("Jet OLEDB:Engine Type", "5");
// Food SQL Query
string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
{
dataGridView1.Columns.Clear();
dataGridView1.RowTemplate.Height = 60;
//====================================\\
dataGridView1.Visible = true;
dataGridView2.Visible = false;
try
{
OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
//DataRow dr;
DataSet ds = new DataSet();
conn.Open();
foodType.Value = "Starter";
foodsDa.Fill(ds, "Food_table");
conn.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Food_table";
dataGridView1.Columns.AddRange(ColFoodQtyStock, ColFoodStatus);
DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.dataGridView1.Columns[0].Width = 420;
this.dataGridView1.Columns[1].Width = 180;
this.dataGridView1.Columns[2].Width = 300;
this.dataGridView1.Columns[3].Width = 308;
// ColStatus
ColFoodStatus.HeaderText = "Status";
ColFoodStatus.Name = "ColFoodStatus";
// ColQtyStock
ColFoodQtyStock.HeaderText = "Quantity In Stock";
ColFoodQtyStock.Name = "ColFoodQtyStock";
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
}
Could someone help me to modify the code on my click event button to solve this problem please
Thanks in advance
lapeci
if you want columns added at runtime (although Qty in Stock sounds like a database column) then the easiest way is to add them directly to your dataset.
...
foodsDa.Fill(ds, "Food_table");
//add extra column structures to dataset
ds.Tables["Food_table"].Columns.Add(new DataColumn("ColFoodQtyStock", System.Type.GetType("System.Int32")));
ds.Tables["Food_table"].Columns.Add(new DataColumn("ColFoodStatus", System.Type.GetType("System.String")));
//loop through all the rows and add the data to the new columns dynamically
for (int i = 0; i < ds.Tables["Food_table"].Rows.Count; i++)
{
ds.Tables["Food_table"].Rows[i]["ColFoodQtyStock"] = 0;
ds.Tables["Food_table"].Rows[i]["ColFoodStatus"] = "Always On Stock";
}
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Food_table";
DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridView1.Columns[0].Width = 420;
dataGridView1.Columns[1].Width = 180;
dataGridView1.Columns[2].Width = 300;
dataGridView1.Columns[3].Width = 308;
}
catch (Exception ex)
...
PS: you don't need to call .Open() or Close() on a connection when using a dataAdapter as it is done automatically.
精彩评论