c# - 如何在 C# 中更新 DataGridView 中 CurrentRow 的目标单元格?
问题描述
在我的 Windows 窗体应用程序中,DataGridView
从TextChanged_Event
文本框(条形码读取功能)更新。每个新行都有像Product Name、Quantity、Unit Price和Total Amount这样的值。Product Name和Unit Price值来自数据库,而我为 Quantity 设置了默认值1。Total Amount的值是Quantity和Unit Price的乘积。
到目前为止一切正常,我可以为每件商品的销售数量使用默认的 1 值。但是,如果我想多次销售一件商品,则需要将此默认数量值替换为自定义值。
为了简化这个过程,我使用了一个带有单个文本框和一个按钮的子表单,它允许我的用户为 Quantity 列放置一个自定义值,该值应该替换 Quantity 列的默认值 1。
我尝试了不同的方法,但没有运气。因为我在销售过程的那部分显示子表单,应用程序必须在 DataGridView 中查找重复的产品。如果 DataGridView 中已经存在一个项目,应用程序会要求再次添加它,当用户单击“是”按钮时,将显示子窗体。
因此,它使我的逻辑毫无意义,因为在子表单上显示并使用自定义数量值文本框后,我无法访问父表单上的变量以用此自定义数量替换其默认值 1。
我的代码运行良好,但我只需要对逻辑进行一些改进,以便我可以轻松地将默认数量值替换为子表单中的自定义值。
这是我的代码:
public void FillCurrentSoldItemGrid(string barCode)
{
string columnList = "ProductName, UnitPrice";
DataRow dr = NewSale.SelectByItemCode(columnList, barCode);
if (dr == null)
{
//show error message
return;
}
// check duplicate value in DataGridView
string productName = dr[0].ToString();
if (dgvSoldItems.RowCount > 0)
{
foreach (DataGridViewRow dgvRow in dgvSoldItems.Rows)
{
if (dgvRow.Cells[0].Value.ToString().CompareTo(productName) == 0)
{
if (DataAccess.AskUser("This item is already present in the grid?", "Confirm Item Quantity Update") == true)
{
//my child form code
using (var customSaleQuantity = new frmCustomSaleQuantity())
{
var result = customSaleQuantity.ShowDialog();
if (result == DialogResult.OK)
{
//need to assign this value of the customQuantity string to int quantity which is holding a default 1 value)
string customQuantity = customSaleQuantity.GetCustomQuantity;
}
}
}
}
}
}
//value of string customQuantity to be assigned to int quantity.
int quantity = 1;
int unitPrice = Convert.ToInt32(dr[1]);
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvAllSoldItems);
row.Cells[0].Value = productName;
row.Cells[1].Value = quantity;
row.Cells[2].Value = unitPrice;
row.Cells[3].Value = (quantity * unitPrice);
dgvAllSoldItems.Rows.Add(row);
}
这是我的子表单中的代码:
public string GetCustomQuantity
{
get;
set;
}
private void btnProceedWithNewItemQuantity_Click(object sender, EventArgs e)
{
this.GetCustomQuantity = txtChooseSaleQuantity.Text.Trim();
this.Close();
}
我是 c# 的新手,如果检测到重复项目,我不知道如何用自定义值替换默认的数量 1 值。这是一项非常重要的任务,我的截止日期快结束了。
任何帮助将不胜感激。
解决方案
有很多方法可以提高逻辑;但是,一种方法是应用面向对象编程 (OOP) 概念。定义一个静态类来保存全局变量、字段和函数。一旦用户在子表单中输入数据,将该信息传递给静态类 getter/setter 方法。此外,在您的静态类中,您可以定义一个公共函数来处理数据。不要忘记刷新 GridView 以查看更新的信息。我推荐使用静态类的主要原因,您可以从应用程序的任何位置调用公共属性和方法,并且您不需要创建类对象的实例。
static class Globals
{
// global int
public static int counter;
// global function
public static string HelloWorld()
{
return "Hello World";
}
}
Globals.counter = 10;
int localcounter = Globals.counter;
string somestring = Globals.HelloWorld();
Globals.counter2 = 30;
int localcounter2 = Globals.counter2;
推荐阅读
- python - easy_install conda 因找不到合适的 Requirement.parse('menuinst') 错误而失败
- php - Bash 中的卷曲选项
- json - 返回 response()->json() 时要声明什么返回类型
- r - 如何根据其他列生成列的运行总计
- yii2 - YII2 登录资产 CSS 未加载
- node.js - Node.js > 'findOne' of undefined < LocalStrategy from Passport
- javascript - 未捕获的 TypeError: (0 , _module) 不是函数
- excel - 与列中的先前条目匹配,复制行中的其余数据
- sql - Laravel Auth 注册用户失败
- c# - 如何在 Xamarin 上使用 AppSettings 插件?我希望能够保存用户在 EmailEntry 上输入的内容