【问题标题】:devexpress gridView.Rows and cells?devexpress gridView.Rows 和单元格?
【发布时间】:2016-10-03 20:01:41
【问题描述】:

目前我一直在使用Devexpress Datagridview。所以我需要将下面的代码转换成它的 Devexpress Datagridview 等价物:

        int id = e.RowIndex;
        DataGridViewRow row = dgvproductMenu.Rows[id];


        if (dgvproductMenu.Columns[e.ColumnIndex].Name == "Update")
        {
            product ObjP = new product
            {
                Product_id = row.Cells[2].Value.ToString(),
                Name = row.Cells[3].Value.ToString(),
                Description = row.Cells[4].Value.ToString(),
                Price = Convert.ToDouble(row.Cells[5].Value),
                Uom = row.Cells[6].Value.ToString(),
                Quantity =Convert.ToInt32( row.Cells[7].Value.ToString()),
                Tax = row.Cells[8].Value.ToString()
            };
            frmAddProduct fm = new frmAddProduct(ObjP);
            fm.ShowDialog();
            this.fillGrid();

我想使用 Devexpress 扩展 (gridview) 来实现。

【问题讨论】:

  • 为什么不查看 devexpress 文档?
  • DataRow 行 = gdvInvoiceSummary.GetDataRow(gdvInvoiceSummary.GetSelectedRows()[0]); if (e.Button.Caption == "update") { invoiceSummary obj = new invoiceSummary { //CustomerName=row. }; frmAddinvoice fm = new frmAddinvoice(); fm.ShowDialog(); }
  • 也许从获取和设置单元格值文档开始:documentation.devexpress.com/#windowsforms/CustomDocument753

标签: c# oop devexpress


【解决方案1】:

我假设您正在尝试基于某个偶数捕获行数据,而您在问题中已将其省略。假设您使用的是 POCO/域对象,那么可以采用的方法是引入绑定源并使用绑定源的 .Current 属性来确定突出显示的行。

或者,如果您对此不以为然,则网格视图的.GetRow(i) 事件将返回第 i 行中的对象。

从这里,您可以简单地将其转换为您的 POCO:

MyClrObject mo = (MyClrObject)bindSourceClr.Current;

MyClrObject mo = (MyClrObject)gridView1.GetRow(gridView1.FocusedRowHandle);

我认为这对于您的后续操作来说也是一种更简洁的方法,您可以在其中提取列值。您可以使用 Intellisense 来获取确切的属性,而不是假设任何关于列顺序的内容:

product ObjP = new product
{
    Product_id = MyClrObject.ProductId,
    Name = MyClrObject.Name,
    Description = MyClrObject.Description
};

如果您正在使用数据表...考虑切换到域对象。这并不总是最好的方法,但有很多优点。

【讨论】:

    【解决方案2】:

    我认为在 XtragridControl 中为 devexpress 获取 gridview 行中的数据是一个清晰的代码。

      if (gridView1.RowCount >= 1)
       {
                List<PurchaseChild> purchaseChild = new List<PurchaseChild>();
    
                PurchaseParent purchaseParent = new PurchaseParent()
                {
                    PurchaseDate = Convert.ToDateTime(txtPurchaseDate.EditValue),
                    PurchaseInvoice = txtInvoice.EditValue.ToString(),
                    CompanyInvoice = txtCompanyInvoice.EditValue.ToString(),
                    SupplyerId = cmbSupplyer.EditValue.ToString(),
                    DelivaryBy = txtDelivaryBy.EditValue.ToString(),
                    CreateBy = Settings.Default.UserName,
                    CreateDate = DateTime.Now,
                    PcName = txtComputerName.EditValue.ToString()
                };
    
                for (int i = 0; i < gridView1.DataRowCount; i++)
                {
                 purchaseChild.Add(new PurchaseChild()
                    {
                PurchaseInvoice = txtInvoice.EditValue.ToString(),
                ProductCode = gridView1.GetRowCellValue(i, "ProductCode").ToString(),
                Qty = Convert.ToInt16(gridView1.GetRowCellValue(i, "Qty")),
                ProductPrice = Convert.ToInt16(gridView1.GetRowCellValue(i, "ProductPrice")),
                SellingPrice = Convert.ToInt16(gridView1.GetRowCellValue(i, "SellingPrice")),
                DiscountPrice = Convert.ToInt16(gridView1.GetRowCellValue(i, "DiscountPrice") == null ? 0 : gridView1.GetRowCellValue(i, "DiscountPrice")),
                DiscountAmount = Convert.ToInt16(gridView1.GetRowCellValue(i, "DiscountPrice") == null ? 0 : gridView1.GetRowCellValue(i, "DiscountAmount")),
                TotalAmount = Convert.ToInt16(gridView1.GetRowCellValue(i, "Qty")) * Convert.ToInt16(gridView1.GetRowCellValue(i, "ProductPrice"))
                    });
                }
    

    【讨论】:

      猜你喜欢
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多