列之间的数据网格操作

时间:2016-08-27 19:32:36

标签: c# mysql datagrid

这是我得到的数据网格

+==========+==========+==========+==========+
| Product  |  Price   | quantity |   Total  |
+==========+==========+==========+==========+

所以我从MySQL数据库获得了产品和价格(表1)。并且用户在数量单元格中输入数字,程序计算总计(总计=价格*数量)将其保存在表2中(在MySQL中)。

这是一个例子

+==========+==========+==========+==========+
| Product  |  Price   | quantity |   Total  |
+==========+==========+==========+==========+
|  AAAAA   |    30    |    2     |    60    |
+==========+==========+==========+==========+

更新

我像这样添加CellEditEndding处理程序

private void Produit_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = ((DataView)Produit.ItemsSource).ToTable();
        foreach (DataRow row in dt.Rows)
        {
            row["total"] = (Convert.ToDouble(row["price"]) * Convert.ToDouble(row["quantity"]));
        }
        Produit.ItemsSource = dt.DefaultView;
    }

当我编辑数量单元格时,每个东西都返回0。

注意:当我填写DataGrid时,我用zoros填充总数

1 个答案:

答案 0 :(得分:0)

据我了解您的问题,您想要询问用户只需输入产品代码和数量,然后价格和总价格会自动添加到data grid view。为此,您需要在Label field TemplateField的{​​{1}}的帮助下添加data grid column

      <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblprice" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
 <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblTotalPrice" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    </Columns>

然后访问标签到服务器端

Label lblprice= (Label)row.FindControl("lblprice");
Label lblTotalPrice= (Label)row.FindControl("lblTotalPrice");

现在只需将值分配给这些标签。

相关问题