[英]Updating Excel sheet with +1 or -1 in column 'stock'
https://imgur.com/a/OKZPMYl我正在使用此代碼將 excel 文件加載到 datagridview 中
private void Loadexcel_Click(object sender, EventArgs e)
{
OpenFileDialog importexcel = new OpenFileDialog();
importexcel.Filter = "Excel files | *.xlsx";
if (importexcel.ShowDialog() == DialogResult.OK)
filetextBox.Text = importexcel.FileName;
String connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filetextBox.Text + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(connStr);
string strSQL = "SELECT * FROM [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
DataTable dT = new DataTable();
conn.Open();
try
{
OleDbDataReader dR = cmd.ExecuteReader();
dT.Load(dR);
excelviewgrid.DataSource = dT;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
NewOrder.Enabled = true;
}
}
我正在使用來自用戶的文本框輸入將項目添加到我的列表框中,該文本框輸入應該與添加來自材料描述列的信息和一些額外硬編碼文本的材料編號相匹配。 並且還將輸入循環到 combobox 中的輸入量。 到目前為止,這一切都有效。
private void textBoxInput_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string strCurrentString = textBoxInput.Text.Trim().ToString();
if (strCurrentString != "")
{
if (radioButtonPick.Checked == true && radioButtonNew.Checked == true)
{
foreach (DataGridViewRow row in excelviewgrid.Rows)
{
if (textBoxInput.Text == row.Cells["Material"].Value.ToString())
{
int value = Convert.ToInt32(comboBoxmultiply.Text);
for (int i = 0; i < value; i++)
{
listBoxinput.Items.Add(textBoxInput.Text + " | " + row.Cells["Material Description"].Value + " (-1 New Pick)");
}
comboBoxmultiply.SelectedIndex = 0;
textBoxInput.Clear();
}
}
}
}
但現在我想讓它對於列表框https://imgur.com/a/soeENXs上的每個項目,它會根據選擇或接收將股票列更新 +1 或 -1(這是通過使用正確的事件完成的觸發(如果 radiobuttonxxx == true),如果低於 0 則給出錯誤
private void buttontest_Click(object sender, EventArgs e)
{
String connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filetextBox.Text + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
OleDbCommand ojbCmdSelect = new OleDbCommand("UPDATE [sheet1$] set [Stock] = [Stock] + 1 where [Material] = 114-6776");
ojbCmdSelect.ExecuteNonQuery();
conn.Close();
}
FIXED 進一步看我用一個測試按鈕嘗試了這段代碼,只是 excel 列表中的一個項目(這需要更改為列表框中的每個項目)的材料。
但它給我一個錯誤“System.InvalidOperationException:'ExecuteNonQuery:連接屬性未初始化。'”
編輯:連接問題似乎已通過此代碼修復編輯:2 由 Cetin Basoz 進行了大規模改進和糾正(如下所示)
conn.Open();
string strSQL = "UPDATE [sheet1$] set Stock = stock+1 where Material = '114-6776'";
OleDbCommand ojbCmdSelect = new OleDbCommand(strSQL,conn);
ojbCmdSelect.ExecuteNonQuery();
conn.Close();
現在我需要弄清楚如何制作“where material = listboxitems”編輯:3 也由 Cetin Basoz 提供(如下所示)
我是編碼新手,所以沒有經驗。
正如消息所說,ojbCmdSelect 需要一個連接,而您沒有設置它。 此外,如果您通過它,OleDbCommand 文本本身還會出現另一個錯誤。 您應該使用參數,並且可以單獨或循環進行更新。 IE:
private void buttontest_Click(object sender, EventArgs e)
{
String connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filetextBox.Text + ";Extended Properties=Excel 12.0;";
using (OleDbConnection conn = new OleDbConnection(connStr))
using (OleDbCommand ojbCmdSelect = new OleDbCommand("UPDATE [sheet1$] set [Stock] = [Stock] + 1 where [Material] = @material", conn))
{
ojbCmdSelect.Parameters.Add("@material", OleDbType.VarChar);
conn.Open();
// this part could be in a loop
// for ... {
ojbCmdSelect.Parameters["@material"].Value = "114-6776";
ojbCmdSelect.ExecuteNonQuery();
// }
conn.Close();
}
}
編輯:根據 listbox.items,您正在將項目添加到列表框中,其代碼附加了額外的空間 | 然后描述。 您首先需要獲取每個列表框項的代碼:
private void buttontest_Click(object sender, EventArgs e)
{
String connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filetextBox.Text + ";Extended Properties=Excel 12.0;";
using (OleDbConnection conn = new OleDbConnection(connStr))
using (OleDbCommand ojbCmdSelect = new OleDbCommand("UPDATE [sheet1$] set [Stock] = [Stock] + 1 where [Material] = @material", conn))
{
ojbCmdSelect.Parameters.Add("@material", OleDbType.VarChar);
conn.Open();
foreach (string item in listBoxinput.Items)
{
var itemcode = item.Split('|')[0].Trim();
ojbCmdSelect.Parameters["@material"].Value = itemcode;
ojbCmdSelect.ExecuteNonQuery();
}
conn.Close();
}
}
PS:在現實世界的應用程序中,您可能希望將其綁定到至少具有 Id 和顯示文本的 object ,而不是在列表框中包含此類項目。 從一個長字符串中拆分來獲取 Id 在任何時候都是不可行的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.