如何将行添加到datagridview的的WinForms? [英] How to add rows to datagridview winforms?
本文介绍了如何将行添加到datagridview的的WinForms?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要行添加到的datagridview
。我尝试了很多的可能性,但它不会出现在任何事情。我认为最好的解决方案是创建一个DataTable,然后用它作为数据源为我的GridView控件。我使用的WinForms。请,任何其他的想法表示欢迎。这是我到目前为止已经试过:
公开数据表GetResultsTable()
{
数据表表=新的DataTable();
table.Columns.Add(名的ToString());
table.Columns.Add(颜色的ToString());
DataRow的博士;
博士= table.NewRow();
博士[名称] =迈克;
博士[颜色] =蓝色;
table.AcceptChanges();
返回表;
}
公共无效的GridView()
{
datagridview1.DataSource = NULL;
datagridview1.DataSource =表;
}
解决方案
我发现了两个错误在code:
-
博士[颜色] =蓝色;
列颜色应无间隙博士[颜色] =蓝色;
-
您忘了行添加到表
table.Rows.Add(DR);
你可以试试这个
公开数据表GetResultsTable()
{
数据表表=新的DataTable();
table.Columns.Add(名的ToString());
table.Columns.Add(颜色的ToString());
DataRow的博士= table.NewRow();
博士[名称] =迈克;
博士[颜色] =蓝色;
table.Rows.Add(DR);
返回表;
}
公共无效的GridView()
{
datagridview1.DataSource = GetResultsTable();
}
I want to add rows to a datagridview
. I tried a lot of possibilities, but it doesn't appear anything on it. I think the best solution is to create a datatable, and then to use it as datasource for my gridview. I use winforms. Please, any other idea is welcomed . This is what I have tried so far:
public DataTable GetResultsTable()
{
DataTable table = new DataTable();
table.Columns.Add("Name".ToString());
table.Columns.Add("Color".ToString());
DataRow dr;
dr = table.NewRow();
dr["Name"] = "Mike";
dr["Color "] = "blue";
table.AcceptChanges();
return table;
}
public void gridview()
{
datagridview1.DataSource=null;
datagridview1.DataSource=table;
}
解决方案
i found two mistake in your code:
dr["Color "] = "blue";
Column Color should without spacedr["Color"] = "blue";
You forgot to add row to the table
table.Rows.Add(dr);
you can try this
public DataTable GetResultsTable()
{
DataTable table = new DataTable();
table.Columns.Add("Name".ToString());
table.Columns.Add("Color".ToString());
DataRow dr = table.NewRow();
dr["Name"] = "Mike";
dr["Color"] = "blue";
table.Rows.Add(dr);
return table;
}
public void gridview()
{
datagridview1.DataSource = GetResultsTable();
}
这篇关于如何将行添加到datagridview的的WinForms?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文