返回首页

您好,

我有一个DataGridView的绑定到DataTable(dtDataCSV)。这个DataTable是充满了从Excel文件,其中只有5山坳日期,品名银行,博士,铬,BAL值。 dtDataCSV有7列。我已经在DataGridView列插入第一个组合框。现在,如果我选择说的第一个组合框的第三项......那么,它应该添加另一个组合框,并填写相应的项目。

我该怎么办呢?
下面是我的代码

private void txtSheet_Leave(object sender, EventArgs e)

        {

            try

            {

                if (string.IsNullOrEmpty(txtFileName.Text))

                {

                    MessageBox.Show("Please select file first");

                    return;

                }

                if (string.IsNullOrEmpty(txtSheet.Text))

                {

                    MessageBox.Show("Please write the exact sheet name of excel file");

                    return;

                }

 

                dtDataCSV = new DataTable();

                DataRow dr;

                dtDataCSV.Columns.Add("TranRefNo", typeof(int));

                dtDataCSV.Columns.Add("Date", typeof(DateTime));

                dtDataCSV.Columns.Add("Bank Desc/Supplier Name", typeof(string));

                dtDataCSV.Columns.Add("Account Code", typeof(string));

                dtDataCSV.Columns.Add("Debit", typeof(double));

                dtDataCSV.Columns.Add("Credit", typeof(double));

                if (comboBox1.Text == "Bank Account")

                {

                    dtDataCSV.Columns.Add("Balance", typeof(string));

                }

                

 

             

 

                filHandCSV = new clsFileHandler(txtFileName.Text);

 

                if (filHandCSV.FileInf.Exists)

                {

                    var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", filHandCSV.FileInf);

                    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + txtSheet.Text.Trim().ToString() + "$]", connectionString);

                    DataSet ds = new DataSet();

                    adapter.Fill(ds, "dtref");

                    DataTable dttable = ds.Tables["dtref"];

 

                    //Load CSV file to dtDataCSV

                    int TranRefNo = 1;

                    if (dttable.Rows.Count > 0)

                    {

                       

                            foreach (DataRow dr1 in dttable.Rows)

                            {

                                // if (dr1[1].ToString() != "" && dr1[2].ToString() != "")

                                if (dr1[1].ToString() != "")

                                {

                                    dr = dtDataCSV.NewRow();

                                    dr["TranRefNo"] = TranRefNo;

                                    dr["Date"] = Convert.ToDateTime(dr1[0].ToString()).ToString("dd MMM yyyy");

                                    dr["Bank Desc/Supplier Name"] = dr1[1];

                                    dr["Debit"] = dr1[2];

                                    dr["Credit"] = dr1[3];

                                    dr["Balance"] = dr1[4];

                                    TranRefNo = TranRefNo + 1;

                                    dtDataCSV.Rows.Add(dr);

                                }

                            }

                        

                        

                    }

 



                    TranRefNo = 1;

                 

 

                    dgvLoadExp.DataSource = dtDataCSV;

                    //Formats the date column

                    dgvLoadExp.Columns[1].DefaultCellStyle.Format = "dd-MMM-yyyy";

 

                    // Add a new Column (Account Head) 

                    DataGridViewComboBoxColumn colAccHead = new DataGridViewComboBoxColumn();

                    Expenses objAccHead = new Expenses();

                    

                    DataTable dt = new DataTable();

                    dt.Columns.Add("catid", typeof(int));

                    dt.Columns.Add("Account Head", typeof(string));

                    dr = dt.NewRow();

                    dr["catid"] = -1;

                    dr["Account Head"] = "";

                    dt.Rows.Add(dr);

                    dt.Merge(objAccHead.getAccountHead());

                    

                    colAccHead.DataSource = dt;

                    colAccHead.HeaderText = "Account Head";

                    colAccHead.DisplayMember = "Account Head";

                    colAccHead.ValueMember = "catid";

                    this.dgvLoadExp.Columns.Insert(4, colAccHead);

                    

                    //Use DataPropertyName to get the selected key column as in :- 

                    this.dgvLoadExp.Columns[4].DataPropertyName = "catid";

                  

                }

 

            }

            catch (Exception ex)

            {

 

            }

        }

 

        private void dgvLoadExp_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        {

            if (dgvLoadExp.CurrentCell.ColumnIndex == 4)

            {

                ComboBox cmb = e.Control as ComboBox;

                cmb.SelectedValueChanged += new EventHandler(cmb_SelectedValueChanged);

            }

        }

 

        void cmb_SelectedValueChanged(object sender, EventArgs e)

        {

            DataGridViewComboBoxColumn colSubAccHead = new DataGridViewComboBoxColumn();

 

            Expenses objSubAccHead = new Expenses();

            colSubAccHead.DataSource = objSubAccHead.getSubAccountHead(((ComboBox)sender).SelectedValue.ToString());//Error at this line

            colSubAccHead.HeaderText = "Sub Account Head";

            colSubAccHead.DisplayMember = "SubHead";

            colSubAccHead.ValueMember = "id";

            this.dgvLoadExp.Columns.Insert(5, colSubAccHead);

           

        }
objSubAccHead.getSubAccountHead()返回一个DataTable的AccountHead ID被选中的值,即CATID

任何帮助将非常感激
谢谢

回答

评论会员:游客 时间:2012/02/05
samip什雷斯塔:嗨添加第二个组合框旁边在其selectedvaluechanged事件的第一个组合框...不断增加组合框。但我只需要两comboboxes.So我所做的是第二个ComboBox中添加txtsheet_Leave事件。这里是代码。{C}它显示了正确的价值观。但是,如果用户选择了两个组合框,然后改变了第一,第二个是不再有效。然后,它显示了一个错误:System.ArgumentException:DataGridViewComboBoxCell中值是无效的要替换此默认对话框,请处理DataError事件。我应该怎么做呢?任何帮助将不胜感激。感谢
理查德MacCutchan
评论会员:游客 时间:2012/02/05
您好,你也可以尝试imgsrc=]


 60; 我想对你的要求一些代码

检查我的代码


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script language ="javascript" >

 



    

    </script>

</head>

<body >

    <form id="form1" runat="server">

    <div>

        <asp:datalist id="DataList1" runat="server" repeatcolumns="5" xmlns:asp="#unknown">

            RepeatDirection ="Horizontal" onitemcommand="DataList1_ItemCommand" 

            onitemdatabound="DataList1_ItemDataBound" >

        <HeaderTemplate >

        </HeaderTemplate>

        <itemtemplate>

         <table width="20" height="20">

          <tr>

            <td>

                <asp:linkbutton id="LinkButton1" runat="server" commandname="book" commandargument="<%#Eval("vid") %>"><img src="Images/Winter.jpg" width="19" height="19" /></asp:linkbutton>

            </td>

          </tr>

         </table>

        </itemtemplate>

        <footertemplate>

        

        </footertemplate>

        </asp:datalist>

    </div>

    </form>

</body>

</html>


和的代码behin文件包含以下代码

{体C3}在这里,你要检查数据库中的值在DataBound事件
我希望你可以理解我所做的。

最佳