日期:2014-05-17  浏览次数:20736 次

datagridview增加的列顺序改变导致查询出错问题,请达人指教
数据库查询后数据绑定到datagridview上,然后在最后增加一列DataGridViewCheckBoxColumn,通过设置DisplayIndex=0显示到第一列,第一次执行查询正常显示,第二次就报错:指定的参数已超出有效值的范围。
参数名: columnIndex
,去掉设置DisplayIndex=0则一切正常。网上查不多相关问题,特来指教

 public void searchData()
{
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            string t1 = "";
            string db = this.comboBoxEx3.Text.ToString().Trim();
            string fl = this.comboBoxEx1.Text.ToString().Trim();
            if (fl != "")
            {
                t1 = " and " + db + ".dbo.CustomerClass.cCCName='" + fl + "'";
            }

            this.dgv_CS.Columns.Clear();
            
            isGetData("select " + db + ".dbo.Customer.cCuscode as 客户编码," + db + ".dbo.Customer.cCusName as 客户名称," + db + ".dbo.CustomerClass.cCCName as 客户分类 from " + db + ".dbo.Customer"
            + " left join " + db + ".dbo.CustomerClass on  " + db + ".dbo.Customer.cCCCode=" + db + ".dbo.CustomerClass.cCCCode where 1=1" + t1 + " order by dbo.Customer.cCuscode DESC");

            
            //将当前光标更改回原来的状态
            Cursor.Current = currentCursor;
}
private void isGetData(string sql)
{
            DataTable dt = new DataTable();
            dt = GetData(sql);

            bindingSource1.DataSource = dt;
            dgv_CS.DataSource = bindingSource1;

            //dgv_CS.AutoResizeColumns(
                  //DataGridViewAutoSizeColumnsMode.AllCells);
    

            DataGridViewCheckBoxColumn dgvCheckColumn = new DataGridViewCheckBoxColumn();
            dgv_CS.Columns.Insert(3, dgvCheckColumn);