【发布时间】:2016-05-22 20:21:07
【问题描述】:
我想在每次单击按钮时向 gridcontrol 添加新行。我尝试了很多方法,但没有成功。我正在发送我的代码。
private void B_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
int productId = (int)bt.Tag;
AddProductDataContext db = new AddProductDataContext();
decimal Quantity;
decimal.TryParse(txtCalculator.Text, out Quantity);
var results = from inv in db.Inventories
where inv.RecId == productId
select new
{
inventoryName = inv.InventoryName,
Quantity,
Total = Quantity * inv.InventoryPrice
};
DataTable dt = new DataTable();
dt.Columns.Add("inventoryName");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");
foreach (var x in results)
{
DataRow newRow = dt.Rows.Add();
newRow.SetField("inventoryName", x.inventoryName);
newRow.SetField("Quantity", x.Quantity);
newRow.SetField("Total", x.Total);
}
gridControl1.DataSource = dt;
gridView1.AddNewRow();
}
【问题讨论】:
-
首先你在第一个问题中使用我的代码stackoverflow.com/questions/37187344/…
-
在gridControl1.DataSource = dt之后添加gridControl1.DataBind();
-
你必须改变 gridControl1.DataSource = dt;到 gridView1.DataSource = dt;再试一次
-
是的,你说得对,我使用了你的代码,因为我不知道如何编辑代码部分。
-
gridControl 没有 DataBind() 选项,gridview 不采用 DataSource,因为它是只读的。
标签: c# entity-framework-6 devexpress