我是wpf的新手,我正在尝试设置 DataGrid
“列” DataGridNumericUpDownColumn
“从数据库。我得到的数据是 DataTable
把它绑在 DataGrid
.
那是我拿东西的table。
CREATE TABLE Items
(
ITEM varchar(64),
PART_NUMBER varchar(64),
TYPE varchar(64),
MANUFACTURER varchar(64),
PRICE decimal(32,2) NOT NULL,
PRIMARY KEY(ITEM, PART_NUMBER, TYPE, MANUFACTURER),
FOREIGN KEY (MANUFACTURER, TYPE)
REFERENCES ManufacturerType(MANUFACTURER,TYPE)
ON UPDATE CASCADE
ON DELETE CASCADE
);
c代码
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
//////////////////////////////////////////////////////////////////////////////////////////
// ITEMS TABLE //
//////////////////////////////////////////////////////////////////////////////////////////
StringBuilder command = new StringBuilder();
command.Append("SELECT MIN(ii.IMAGE_PATH) AS IMAGE_PATH, MAX(i.ITEM) AS ITEM, i.PART_NUMBER AS PART_NUMBER, ");
command.Append("MAX(i.TYPE) AS TYPE, MAX(i.Manufacturer) AS MANUFACTURER, i.PRICE FROM ");
command.Append("Items i ");
command.Append("LEFT JOIN Item_Images ii ON i.PART_NUMBER = ii.PART_NUMBER ");
command.Append("GROUP BY i.PART_NUMBER, i.ITEM");
//SETUP COLUMNS FOR ITEMS TABLE
DataTable dt = mysql_database.queryDataFromDatabaseWithSQLCommand(command.ToString());
dgItems.Columns.Add(createImageColumn("IMAGE", new Binding("IMAGE_PATH"), new Size(32, 32)));
foreach (DataColumn dcol in dt.Columns)
{
if (dcol.ColumnName.ToUpper() == "PRICE")
{
dgItems.Columns.Add(createDataGridNumericUpDownColumn(dcol.ColumnName.ToUpper(), "C"));
}
else
{
DataGridTextColumn dgc = new DataGridTextColumn();
dgc.Header = dcol.ColumnName.ToUpper();
dgc.Binding = new Binding(dcol.ColumnName.ToUpper());
dgc.IsReadOnly = true;
dgItems.Columns.Add(dgc);
}
}
dgItems.ItemsSource = dt.DefaultView;
}
用于创建datagridnumericupdowncolumn的代码
public DataGridNumericUpDownColumn createDataGridNumericUpDownColumn(string header, string format)
{
DataGridNumericUpDownColumn dtemp = new DataGridNumericUpDownColumn();
dtemp.Minimum = 0.00;
dtemp.Binding = new Binding("{" + header + "}");
dtemp.StringFormat = format;
dtemp.Header = header;
return dtemp;
}
但是,当我运行应用程序时 numericupdown
是空的。不会从数据表中设置该值。
运行工具:
这是存储在数据库中的内容。
数据库中的数据行:
我不知道该怎么做。感谢您的帮助!谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!