Dev控件GridControl實現CheckBox列和ComBox列


1、在sql語句中添加空白行,如select c1,c2 null c3 from xxx;

2、將sql語句查詢結果與gdc綁定CmmFrm.BestFitGridViewColumnsWidth(gdc_lines, sql,2);

3、將上述空白列綁定為RepositoryItemCheckEdit控件,並添加事件

  RepositoryItemCheckEdit re = new RepositoryItemCheckEdit();            

  re.QueryCheckStateByValue += new DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventHandler(Re_QueryCheckStateByValue);//定義這個事件是關鍵,因為就靠它了  

  gdv_lines.Columns[2].ColumnEdit = re;

4、事件實現:

     void Re_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
        {
            string val = "";
            if (e.Value != null)
            {
                val = e.Value.ToString();
            }
            else
            {
                val = "";//默認為不選中
            }
           switch (val)
            {
                case"True":
                    e.CheckState = CheckState.Checked;
                    break;
                case"False":
                    e.CheckState = CheckState.Unchecked;
                    break;
                case"Yes":
                    goto case"True";
                case"No":
                    goto case"False";
                case"1":
                    goto case"True";
                case"0":
                    goto case"False";
                default:
                    e.CheckState = CheckState.Unchecked; //默認狀態,如果默認選擇,則改為Checked
                    break;
            }
            e.Handled = true;
        }

5、整個gdv控件要設為可編輯,但除了chekbox列外,其它列要禁止出現編輯,這要靠gdc控件的ShowingEditor事件實現

private void gdv_lines_ShowingEditor(object sender, CancelEventArgs e)        

{

            if (gdv_lines.FocusedColumn.AbsoluteIndex != 2)

                e.Cancel = true;

}

6、遍歷、檢索

for (int i = 0; i < gdv_lines.DataRowCount; i++)
            {
                DataRow dr=gdv_lines.GetDataRow(i);
                string ck = dr[2].ToString();
                if (ck == "True")
                {

        ...

      }

7、程序設置選擇狀態

bool res = true;            

if (gdv_lines.GetDataRow(0)[2].ToString() == "True") res = false; 

for (int i = 0; i < gdv_lines.DataRowCount; i++)  gdv_lines.GetDataRow(i)[2] = res;

 

///////////////////////////////////////////////////   

另外,添加CombBox的方法,還不完善            

                //  gdvLevel.BeginUpdate(); //開始視圖的編輯,防止觸發其他事件

                //  gdvLevel.BeginDataUpdate(); //開始數據的編輯

                CmmFrm.BestFitGridViewColumnsWidth(gdcLevel, lvels, 1, false, false);

                gdcLevel.DataSource = lvels;

                gdvLevel.Columns[0].Visible = false;

                RepositoryItemComboBox riCombo = new RepositoryItemComboBox();

                riCombo.Items.AddRange(new string[] { "夏", "目視", "目視以下", "(含)以下" });

                gdcLevel.RepositoryItems.Add(riCombo);

                gdvLevel.Columns["其它"].ColumnEdit = riCombo;

     //目前這種添加combbox的方法有缺陷,主要表現在1、不能設置為DorpDownList樣式。2、選擇后要敲回車,選擇才能現在Cell中,否則焦點移走,選擇就消失。

     //有兩篇文章解決這個問題,有時間梳理下

    //https://yq.aliyun.com/articles/55052
    //https://www.cnblogs.com/lonelyxmas/p/3906496.html

 

                RepositoryItemCheckEdit riCkbox = new RepositoryItemCheckEdit();

                riCkbox.ValueChecked = false;

                gdcLevel.RepositoryItems.Add(riCkbox);

                gdvLevel.Columns["選擇"].ColumnEdit = riCkbox;

                gdvLevel.EndDataUpdate();//結束數據的編輯

                gdvLevel.EndUpdate();   //結束視圖的編輯

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM