在 WPF C# 中将单个项目添加到数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19633361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add individual items to datagrid in WPF C#
提问by Kamal
I have a database with Table name "Product_Master" with column headers "ProductID, ProductCode, ProductName, ProductDescription, LandingPrice, SellingPrice, Stock, ProductCategory".
我有一个数据库,表名为“Product_Master”,列标题为“ProductID、ProductCode、ProductName、ProductDescription、LandingPrice、SellingPrice、Stock、ProductCategory”。
I have a datagrid in my wpf window.
我的 wpf 窗口中有一个数据网格。
I am able to fill datagrid will all the values from the database.
我能够将数据库中的所有值填充到数据网格中。
The code is below.
代码如下。
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);
}
I would like to customize my datagrid with colomn names "Number, ProductCode, ProductName, Quantity, Tax, Total", and would like to add individual values from different tables.
我想使用列名称“Number、ProductCode、ProductName、Quantity、Tax、Total”自定义我的数据网格,并想添加来自不同表的各个值。
How to do the same.
如何做同样的事情。
Added below
在下面添加
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);
}
}
回答by Tafari
Try this for customization in your .xaml:
尝试在您的 .xaml 中进行自定义:
First set AutoGenerateColumns, then add columns you need with proper bindings.
首先设置 AutoGenerateColumns,然后使用适当的绑定添加您需要的列。
DataGrid ItemsSource="{Binding Products}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Column_name_in_table}"/>
</DataGrid.Columns>
This for adding the rows in code-behind .xaml.cs :
这用于在代码隐藏 .xaml.cs 中添加行:
DataRow newRow = dt.NewRow();
newRow["ProductID"] = txtBox1.Text;
newRow["ProductCode"] = txtBox2.Text;
.
.
.
dt.Rows.Add(newRow);
If you want to the DataGridto be notified with the changes in DataTable, set the grid*ItemsSource* to the DataViewor dt.DefaultView()
如果您希望DataGrid收到DataTable更改的通知,请将grid* ItemsSource* 设置为DataView或dt.DefaultView()
回答by dev hedgehog
Here is a great tutorial of how to use DataGrid.
这是一个关于如何使用 DataGrid 的很棒的教程。
http://wpftutorial.net/DataGrid.html
http://wpftutorial.net/DataGrid.html
Set AutoGenerateColumns="False" if you want to have your custom columns defined.
如果要定义自定义列,请设置 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>
In this examle header name is Image.
在此示例中,标题名称是 Image。
Have fun.
玩得开心。

