我有一个窗口窗体与1个文本框中,用户将键入ProductId,按回车键,并在此基础上,产品将被添加到网格视图。
现在,当新行被添加到网格视图中时,我想为每一行添加textbox,但问题是我看到一个不可编辑的单元格,并且无法生成textbox。
代码:
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;
}
}
更新
在最后一列,我想让用户添加最后的金额在一个考虑折扣的每个产品的文本框中。
发布于 2019-03-26 08:52:31
尝试以下操作,它将绑定并显示来自您的xls/xlsx表的结果,并根据请求添加一个textbox列。
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
块包装以捕获任何额外的异常。
https://stackoverflow.com/questions/55340263
复制相似问题