代码之家  ›  专栏  ›  技术社区  ›  Learning-Overthinker-Confused

向datagridview的每一行添加textbox时出现问题

  •  1
  • Learning-Overthinker-Confused  · 技术社区  · 5 年前

    我有一个带有1个文本框的windows窗体,用户将在其中键入ProductId,按enter键,并基于该结果将产品添加到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;
            }
        }
    

    更新

    enter image description here

    在最后一列中,我希望允许用户在一个文本框中添加最终金额,考虑到每个产品的折扣。

    0 回复  |  直到 5 年前
        1
  •  0
  •   Magic Mick    5 年前

    尝试以下操作,它应该绑定并显示来自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 块捕获任何其他异常。

    enter image description here