【问题标题】:How to add to gridcontrol a new row at every click如何在每次单击时向 gridcontrol 添加新行
【发布时间】: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


【解决方案1】:

你得打电话

gridView1.DataBind();

设置数据源后

【讨论】:

  • Devexpress 网格不允许 DataBind,因此无法使用。
【解决方案2】:

您可以使用下面的代码在 GridControl 中添加新行:

gridView1.AddNewRow();

int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
if (gridView1.IsNewItemRow(rowHandle))
{
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName1"], val1);
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName2"], val2);
    gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ColumnName3"], val3);
}

您必须使用您的值更改列名称和 val1、val2、val3。

【讨论】:

  • 是的,这是给 devexpress 的,但是你使用 DevExpress 的 GridControl 吗?
  • 您的代码有什么问题?你有错误吗?
  • 我解决了问题并发布了解决方案。感谢您的宝贵帮助。
  • 好!我很高兴听到它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
相关资源
最近更新 更多