我对此进行了搜索,但是我发现的数据是5-6岁,并且 Bind();功能似乎不再有效。
所以我有这个函数可以填充我的DataGridView:
private void FillTable(string location, string supplier, string item) {
string sql = "";
string connString = "<connection info";
// If ONLY Location is entered
if (location != "" && supplier == "" && item == "") {
sql = "SELECT " +
" location_id AS 'Location', " +
" supplier_id AS 'Supplier', " +
" item_id AS 'Item', " +
" recommended_qty_to_order AS 'Recommended Qty'" +
" FROM PorgReqs " +
" WHERE location_id = @LocationID " +
" ORDER BY supplier_id, item_id";
}
// Creates Connection, Sets Variables in Connection and tries to load table
using (SqlConnection conn = new SqlConnection(connString)) {
SqlCommand cmd = new SqlCommand(sql, conn);
// Sets Variables
cmd.Parameters.AddWithValue("@LocationID", txtLocation.Text);
cmd.Parameters.AddWithValue("@SupplierID", txtLocation.Text);
cmd.Parameters.AddWithValue("@ItemID", txtLocation.Text);
// Opens Connection
conn.Open();
try
{
// Creates Reader Object
SqlDataReader sqlDataReader = cmd.ExecuteReader();
// Fill the list box with the values retrieved
if (sqlDataReader.HasRows)
{
DataTable dt = new DataTable();
dt.Load(sqlDataReader);
//MessageBox.Show(dt.ToString());
gridData.DataSource = dt;
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
}
} // End Function
我的搜索表明我需要将数据集绑定到网格,然后才能为该列设置DataPropertyName,以便正确加载它们。
请注意。我的目标是将数据加载到网格中,允许编辑“ addl qty”列,该列将反映在“ Final Qty”列中。然后单击它的“批准”复选框,它将信息写入一个单独的数据库,然后在单击“购买”按钮时进行处理。我以为DataGridView控件不是最好的方法,请在进入兔子洞之前让我知道。
答案 0 :(得分:0)
我试图将DataTable绑定到DataGridView,以便按Designer格式化表格,并添加其他列。但是,这无法完成。
相反,我将DataTable加载到DataGridView中,然后将所需的列添加到DataGridView中。
这不是最优雅的,但是可以用...
private void FillTable(string location, string supplier, string item) {
string sql = "";
string connString = "<connect info>";
// If ONLY Location is entered
if (location != "" && supplier == "" && item == "") {
MessageBox.Show("Only Location");
sql = "SELECT " +
" location_id AS 'Location', " +
" supplier_id AS 'Supplier', " +
" item_id AS 'Item', " +
" recommended_qty_to_order AS 'Rec Qty'" +
" FROM PorgReqs " +
" WHERE location_id = @LocationID " +
" ORDER BY supplier_id, item_id";
}
// Creates Connection, Sets Variables in Connection and tries to load table
using (SqlConnection conn = new SqlConnection(connString)) {
SqlCommand cmd = new SqlCommand(sql, conn);
// Sets Variable
cmd.Parameters.AddWithValue("@LocationID", txtLocation.Text);
// Opens Connection
conn.Open();
try
{
// Creates Reader Object
SqlDataReader sqlDataReader = cmd.ExecuteReader();
// Fill the list box with the values retrieved
if (sqlDataReader.HasRows) {
// Create DataTable
DataTable dt = new DataTable();
// Add Columns to DataTable
dt.Columns.Add("Location");
dt.Columns.Add("Supplier");
dt.Columns.Add("Item");
dt.Columns.Add("Rec Qty");
dt.Load(sqlDataReader);
gridData.DataSource = dt;
DataGridViewTextBoxColumn txtAddlQty = new DataGridViewTextBoxColumn();
txtAddlQty.Name = "AddlQty";
txtAddlQty.HeaderText = "Add'l Qty";
txtAddlQty.Width = 75;
gridData.Columns.Insert(4, txtAddlQty);
DataGridViewTextBoxColumn txtFinalQty = new DataGridViewTextBoxColumn();
txtFinalQty.Name = "FinalQty";
txtFinalQty.HeaderText = "Final Qty";
txtFinalQty.Width = 75;
gridData.Columns.Insert(5, txtFinalQty);
DataGridViewComboBoxColumn cboAction = new DataGridViewComboBoxColumn();
cboAction.Name = "Action";
cboAction.HeaderText = "Action";
cboAction.Items.AddRange("Buy", "Transfer", "Not Buy");
cboAction.Width = 75;
gridData.Columns.Insert(6, cboAction);
DataGridViewCheckBoxColumn chkApprove = new DataGridViewCheckBoxColumn();
chkApprove.Name = "Approve";
chkApprove.HeaderText = "Approve";
chkApprove.Width = 50;
gridData.Columns.Insert(7, chkApprove);
gridData.Columns["Location"].Width = 60;
gridData.Columns["Supplier"].Width = 60;
gridData.Columns["Item"].Width = 125;
gridData.Columns["Rec Qty"].Width = 75;
// Adds default value to the Action column
foreach (DataGridViewRow row in gridData.Rows) {
row.Cells["Action"].Value = "Buy";
}
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
}
} // End Function