我有三个文本框。我需要将每个值添加到相应的字段中。Datagrid 有标题“#, ProductName, ProductCode, Tax, Total”
WPF
<DataGrid ItemsSource="{Binding dt}" x:Name="dgrdBilling" MinColumnWidth="100" Grid.Row="1" CanUserReorderColumns="False" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="Cell" Margin="1,0,-1,0">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Width="25" CanUserResize="False" MinWidth="25"/>
<DataGridTextColumn Header="Name"/>
<!--<DataGridTextColumn Header="Name" Binding="{Binding Path=ProductName}"/>-->
<DataGridTextColumn Header="Code"/>
<DataGridTextColumn Header="Quantity"/>
<DataGridTextColumn Header="Price"/>
<DataGridTextColumn Header="Tax1"/>
<DataGridTextColumn Header="Tax2"/>
<DataGridTextColumn Header="Tax3"/>
<DataGridTextColumn Header="Total"/>
</DataGrid.Columns>
</DataGrid>
单击按钮事件以将数据添加到数据网格
private void btnAddProduct_Click(object sender, RoutedEventArgs e)
{
SqlCeCommand com = new SqlCeCommand("SELECT * FROM Products_Master WHERE ProductName =('" + txtAutoProductName.Text + "') OR ProductCode = ('" + txtProductCode.Text + "')", con);
try
{
System.Windows.Controls.DataGrid dg = new System.Windows.Controls.DataGrid();
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;
dg.Items.Add(txtAutoProductName.Text);
dg.Items.Add(txtAutoProductCode.Text);
dg.Items.Add(txtQuantity.Text);
//dgrdBilling.ItemsSource = dt.DefaultView;
//dgrdBilling.Items.Add(dg);
da.Update(dt);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}