【问题标题】:Add individual items to datagrid in WPF C#在 WPF C# 中将单个项目添加到数据网格
【发布时间】:2013-10-28 11:18:42
【问题描述】:

我有一个表名为“Product_Master”的数据库,其列标题为“ProductID、ProductCode、ProductName、ProductDescription、LandingPrice、SellingPrice、Stock、ProductCategory”。

我的 wpf 窗口中有一个数据网格。

我能够将数据库中的所有值都填充到数据网格中。

代码如下。

        SqlCeCommand com = new SqlCeCommand("SELECT ProductCode FROM Products_Master WHERE ProductName =('" + txtAutoProductName.Text + "') OR ProductCode = ('" + txtProductCode.Text + "')", con);
        try
        {
            SqlCeDataAdapter da = new SqlCeDataAdapter();
            da.SelectCommand = com;
            DataTable dt = new DataTable();
            da.Fill(dt);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dt;
            dgrdBilling.ItemsSource = bSource;
            da.Update(dt);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

我想使用列名“数字、产品代码、产品名称、数量、税收、总计”自定义我的数据网格,并想添加来自不同表的单个值。

如何做同样的事情。

添加在下面

    private void txtAutoProductName_TextChanged(object sender, EventArgs e)
    {
        SqlCeCommand com = new SqlCeCommand("SELECT * FROM Products_Master WHERE ProductName =('" + txtAutoProductName.Text + "') OR ProductCode = ('" + txtProductCode.Text + "')", con);
        try
        {
            SqlCeDataAdapter da = new SqlCeDataAdapter();
            BindingSource bSource = new BindingSource();
            DataTable dt = new DataTable();
            DataRow newRow = dt.NewRow();
            da.SelectCommand = com;
            da.Fill(dt);
            bSource.DataSource = dt;
            //dgrdBilling.ItemsSource = bSource;
            dgrdBilling.ItemsSource = dt.DefaultView;
            //dgrdBilling.Items.Add(bSource);
            da.Update(dt);
            newRow["ProductName"] = txtAutoProductName.Text;
            newRow["ProductCode"] = bSource;
            dt.Rows.Add(newRow);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

【问题讨论】:

    标签: c# wpf visual-studio-2012 datagrid


    【解决方案1】:

    尝试在您的 .xaml 中进行自定义:

    首先设置 AutoGenerateColumns,然后使用适当的绑定添加所需的列。

    DataGrid ItemsSource="{Binding Products}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
         <DataGridTextColumn Header="Name" Binding="{Binding Path=Column_name_in_table}"/>
    </DataGrid.Columns>
    

    这用于在代码隐藏 .xaml.cs 中添加行:

    DataRow newRow = dt.NewRow();
    
    newRow["ProductID"] = txtBox1.Text;
    newRow["ProductCode"] = txtBox2.Text;
          .
          .
          .
    
    dt.Rows.Add(newRow);
    

    如果您希望 DataGrid 收到 DataTable 更改的通知,请设置 grid *ItemsSource* 到 DataViewdt.DefaultView()

    【讨论】:

      【解决方案2】:

      这里有一个很好的关于如何使用 DataGrid 的教程。

      http://wpftutorial.net/DataGrid.html

      如果要定义自定义列,请设置 AutoGenerateColumns="False"。

      <DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" >
          <DataGrid.Columns>
              <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
                  <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                          <Image Source="{Binding Image}" />
                      </DataTemplate>
                  </DataGridTemplateColumn.CellTemplate>
                  </DataGridTemplateColumn>
          </DataGrid.Columns>
      </DataGrid>
      

      在此示例中,标题名称为 Image。

      玩得开心。

      【讨论】:

      • 我已经阅读了 b4,但它是否显示了添加单个值的硬件。因为我没有在 xaml 中编写太多代码。对不起。我无法理解该绑定的含义。
      • @KaMaLMoHaN 你所说的个人价值观是什么意思?就像只有一个新行?还是某个单元格中的单个值?
      • 我的意思是当我在两个文本框中输入产品名称和数量时,它的产品代码、产品名称、数量、税收和总计应该显示在数据网格的第一行,然后如果我输入另一个产品,则该产品的相应值应添加到下一行。你知道计费软件。我正在开发那个。
      猜你喜欢
      • 2013-10-07
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多