WPF临时表的作用、使用方法及注意事项

本文介绍了如何在项目中利用临时表进行数据新增,通过实例展示了如何在C#中创建和操作DataTable用于空单录入。注意列类型匹配和表格结构一致性以避免错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、作用

在我们拿到项目需求时,增删查改必不可少,实现方法也多种多样,比如我做的一个项目,就需要用到临时表来做新增等操作,这也就是临时表的作用了。

 

二、使用方法

1.首先要在页面建立表格信息:

2.在方法内编写代码新增一行空表:代码如下

//新增
        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cbo_handler.Text != "" && cbo_comm.Text != "")
                {
                    dt = new DataTable();
                    dt.Columns.Add("xuHao", typeof(int));
                    dt.Columns.Add("commodityID", typeof(int));
                    dt.Columns.Add("code", typeof(string));
                    dt.Columns.Add("number", typeof(string));
                    dt.Columns.Add("name", typeof(string));
                    dt.Columns.Add("InPrice", typeof(string));
                    dt.Columns.Add("newJinJia", typeof(decimal));
                    dt.Columns.Add("retailPrice", typeof(string));
                    dt.Columns.Add("newRetail", typeof(decimal));
                    dt.Columns.Add("PiFaPrice", typeof(string));
                    dt.Columns.Add("newWholesale", typeof(decimal));
                    dt.Columns.Add("VipPrice1", typeof(string));
                    dt.Columns.Add("newVipPrice", typeof(decimal));
                    dt.Columns.Add("note", typeof(string));

                    if (dgCommodity.Items.Count > 0)
                    {
                        dt = ((DataView)dgCommodity.ItemsSource).Table;
                        int j = dt.Rows.Count;
                        for (int i = j; i < j + 1; i++)
                        {
                            dt.Rows.Add();
                            dt.Rows[i]["xuHao"] = i + 1;
                        }
                        dgCommodity.ItemsSource = dt.DefaultView;
                    }
                    else
                    {
                        for (int i = 1; i <= 1; i++)
                        {
                            dt.Rows.Add();
                            dt.Rows[i - 1]["xuHao"] = i;
                            int comm = Convert.ToInt32(cbo_comm.SelectedValue);
                            DataTable dts = myC.Window_Loaded_SelectCommByCommId(comm).Tables[0];
                            string commodityID = dts.Rows[0]["commodityID"].ToString();
                            string code = dts.Rows[0]["code"].ToString();
                            string number = dts.Rows[0]["number"].ToString();
                            string name = dts.Rows[0]["name"].ToString();
                            string retailPrice = dts.Rows[0]["retailPrice"].ToString();
                            string InPrice = dts.Rows[0]["InPrice"].ToString();
                            string PiFaPrice = dts.Rows[0]["PiFaPrice"].ToString();
                            string VipPrice1 = dts.Rows[0]["VipPrice1"].ToString();
                            string note = dts.Rows[0]["note"].ToString();
                            dt.Rows[i-1]["commodityID"] = commodityID;
                            dt.Rows[i-1]["code"] = code;
                            dt.Rows[i-1]["number"] = number;
                            dt.Rows[i-1]["name"] = name;
                            dt.Rows[i-1]["retailPrice"] = retailPrice;
                            dt.Rows[i-1]["InPrice"] = InPrice;
                            dt.Rows[i-1]["PiFaPrice"] = PiFaPrice;
                            dt.Rows[i-1]["VipPrice1"] = VipPrice1;
                            dt.Rows[i-1]["note"] = note;
                        }
                        dgCommodity.ItemsSource = dt.DefaultView;
                    }

                    MessageBox.Show("新增空单成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    cbo_comm.IsEnabled = true;
                }
                else
                {
                    MessageBox.Show("请先把信息填写完整!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

我通过点击新增按钮就能新增一行空单了:

三、注意事项:

dt.Columns.Add("xuHao", typeof(int));

要注意页面表格内的值是否和新增空表表头行的值是否一致"xuHao"后面的typeof括号中的数据类型要和数据库字段保持一致。

以免出现一些不必要的bug!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值