C# 向 DevExpress GridView 添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10147139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding rows to DevExpress GridView
提问by TheGateKeeper
Can someone please help me with this? Why is this so hard when it is so simple for the regular GridView.
有人可以帮我解决这个问题吗?为什么这对于常规 GridView 来说如此简单却如此困难。
I am trying to add rows programmatically. In the normal GridView I would just call the gridView.Rows and add from there, but I can't find that option here.
我正在尝试以编程方式添加行。在普通的 GridView 中,我只会调用 gridView.Rows 并从那里添加,但我在这里找不到该选项。
I tried creating a DataTable and then bind it to it, like so:
我尝试创建一个 DataTable 然后将其绑定到它,如下所示:
DataTable dt = new DataTable();
dt.Columns.Add("IP", Type.GetType("System.String"));
dt.Columns.Add("Port", Type.GetType("System.String"));
dt.Columns.Add("Username", Type.GetType("System.String"));
dt.Columns.Add("Password", Type.GetType("System.String"));
dt.Columns.Add("Working?", Type.GetType("System.Boolean"));
for (int i = 0; i < 20; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Test";
dr[1] = "Test";
dr[2] = "Test";
dr[3] = "Test";
dr[4] = true;
dt.Rows.Add(dr);
}
gcProxies.DataSource = dt;
All this did was add empty rows the the GridControl.
所有这一切都是在 GridControl 中添加空行。
Any ideas?
有任何想法吗?
采纳答案by Brad Rem
Your problem was that you created a column in the designer and then your code behind didn't override it.
您的问题是您在设计器中创建了一个列,然后您的代码没有覆盖它。
In addition to data binding your DataTable to the grid, you can also include unbound columns that either display arbitrary data or display data based on an expression. Here's a basic example for creating an unbound column: How to: Add an Unbound Column
除了将 DataTable 数据绑定到网格之外,您还可以包括显示任意数据或基于表达式显示数据的未绑定列。这是创建未绑定列的基本示例:如何:添加未绑定列
In your sample, you could add the unbound column after you set your datasource:
在您的示例中,您可以在设置数据源后添加未绑定列:
gcProxies.DataSource = dt;
// Create an unbound column.
DevExpress.XtraGrid.Columns.GridColumn unbColumn = gridView1.Columns.AddField("Total");
unbColumn.VisibleIndex = gridView1.Columns.Count;
unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
// Disable editing.
unbColumn.OptionsColumn.AllowEdit = false;
// Specify format settings.
unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
unbColumn.DisplayFormat.FormatString = "c";
gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
Then, here's the event that fills in the unbound columns:
然后,这是填充未绑定列的事件:
void gridView1_CustomUnboundColumnData(object sender,
DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "Total" && e.IsGetData)
e.Value = 100;
}
These examples should get you started to customizing your exact solution.
这些示例应该让您开始定制您的确切解决方案。
回答by kiran
I have faced a problem similar to yours and I have resolved it by adding
我遇到了与你类似的问题,我通过添加解决了它
gcProxies.PopulateColumns();
And one more thing the DataSource is depreciated so kindly switch to ItemSource instead.
还有一件事 DataSource 已折旧,因此请改用 ItemSource。

