我正在制作Windows应用程序
我添加了1个按钮和1个datagridview。
单击该按钮时,数据显示在DB的datagridview中。
这一步,我有一个问题。
我想以编程方式定义DataGridView Columns,如此
SQL查询在
之下SELECT COUNT(*) as Process from Sales.SalesOrderDetail
UNION
SELECT COUNT(*) as Process from Purchasing.ProductVendor
UNION
SELECT COUNT(*) as Process from Person.Address
UNION
SELECT COUNT(*) as Process from Production.WorkOrder
按钮点击事件就像这样
private void btnStart_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AdvConn"].ConnectionString);
SqlCommand cmd = new SqlCommand("UP_SelectTableCount", conn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch{}
finally
{
conn.Close();
}
dt = ds.Tables[0];
metroGrid2.DataSource = dt; }
如何添加或修复我的代码?请帮帮我
感谢。
答案 0 :(得分:1)
将数据添加到datagridview后,您可以编写一个简单的函数来添加像这样的新列
private void AddNewColumns()
{
metroGrid2.Columns.Add("newColumnName", "Column Name in Text");
//To add values to the column you should run a foreach loop
foreach (DataGridViewRow row in metroGrid2.Rows)
{
if (row.Cells[1].Value.ToString()=="1") //Some condition or value
row.Cells[2].Value = "50%";
}
}
这只是添加列而不是添加进度条的想法。但我相信这可能会对您有所帮助,因为您主要关注的是为DGV添加新列。希望这有帮助
添加进度条列Populating a DataGridView with Text and ProgressBars可能会对您有所帮助