我有1个带有1个文本框的Windows窗体,用户将在其中键入ProductId,按Enter键,然后将基于该产品添加到gridview。
现在,当将新行添加到gridview时,我想为每行添加文本框,但是问题是我可以看到单元格不可编辑并且文本框未生成。
代码:
private void txtProductId_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string pathName = txtFilePath.Text;
string fileName = System.IO.Path.GetFileNameWithoutExtension(txtFilePath.Text);
DataTable tbContainer = new DataTable();
string strConn = string.Empty;
string sheetName = fileName;
FileInfo file = new FileInfo(pathName);
if (!file.Exists) { throw new Exception("Error, file doesn't exists!"); }
string extension = file.Extension;
switch (extension)
{
case ".xls":
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
case ".xlsx":
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
break;
default:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
}
string fieldSelector = "[ProductID], [ProductName], [MRP] ";
string query = $"SELECT {fieldSelector} FROM [{sheetName}$A1:F15535] WHERE [ProductID] = {Convert.ToInt32(txtProductId.Text)}";
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(tbContainer);
DataGridViewTextBoxColumn textboxColumn = new DataGridViewTextBoxColumn();
grdProductList.Columns.Add(textboxColumn);
}
e.Handled = true;
}
}
在最后一列中,我希望允许用户在考虑每个产品折扣的文本框中添加最终金额。
答案 0 :(得分:0)
尝试以下操作,它应该绑定并显示xls / xlsx工作表中的结果,并根据要求添加文本框列。
private void txtProductId_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
var pathName = txtFilePath.Text;
var sheetName = Path.GetFileNameWithoutExtension(pathName);
var tbContainer = new DataTable();
var bindingSource = new BindingSource(); //Initiate new binding source
var fieldSelector = "[ProductID], [ProductName], [MRP]";
var query = $"SELECT {fieldSelector} FROM [{sheetName}$A1:F15535] WHERE [ProductID] = {Convert.ToInt32(txtProductId.Text)}";
if (!File.Exists(pathName)) throw new Exception("Error, file doesn't exists!");
grdProductList.AllowUserToAddRows = false; //Prevent user from adding new rows
grdProductList.DataSource = bindingSource; //Link the binding source to your dgview
using (var cnnxls = new OleDbConnection(MsXlConnStr(pathName)))
using (var oda = new OleDbDataAdapter(query, cnnxls))
oda.Fill(tbContainer);
tbContainer.Columns.Add("Discount", typeof(Int32)); //<-- Editable discount column
bindingSource.DataSource = tbContainer; //Display collected results in dgView
e.Handled = true;
}
}
private string MsXlConnStr(string pathName)
{
return new FileInfo(pathName).Extension.ToLower().Contains(".xlsx") ?
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'" :
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
}
注意::我对代码进行了一些缩短和清理,但是我建议使用try catch
换行以捕获其他任何异常。