DevExpress GridControl控件行內新增、編輯、刪除添加選擇框


1.首先到GridControl控件設計里設置屬性Repository    (In-place EditorRepository)  如下圖(CheckEdit可以不添加,這是本人根據自己需求添加的):

         主要設置兩個兩個屬性 NullText,Name

     2.綁定數據

        關鍵代碼如下:

            

DataTable dt = dbHelp.GetDataSql("select TS_NO,TS_NAME ,TS_LONGITUDE,TS_LATITUDE,TS_RANK from  dbo.LPTE_TS");

    grStation.DataSource = null;
    gvStation.Columns.Clear();
    grStation.DataSource = dt;

    gvStation.Columns["TS_NO"].Visible = false;
    gvStation.Columns["TS_NAME"].Visible = true;
    gvStation.Columns["TS_NAME"].Caption = "名稱";
    gvStation.Columns["TS_NAME"].VisibleIndex = 1;
    gvStation.Columns["TS_RANK"].Visible = true;
    gvStation.Columns["TS_RANK"].Caption = "等級";
    gvStation.Columns["TS_RANK"].VisibleIndex = 2;
    gvStation.Columns["TS_LONGITUDE"].Caption = "經度";
    gvStation.Columns["TS_LATITUDE"].Caption = "緯度";

     

    GridColumn addLinkHiper = new GridColumn();
    addLinkHiper.Caption = "新增";
    addLinkHiper.Visible = true;

    addLinkHiper.ColumnEdit = stationAdd;
    gvStation.Columns.Add(addLinkHiper);
    addLinkHiper.VisibleIndex = 5;

    GridColumn editLinkHiper = new GridColumn();
    editLinkHiper.Caption = "編輯";
    editLinkHiper.Visible = true;

    editLinkHiper.ColumnEdit = stationEdit;
    gvStation.Columns.Add(editLinkHiper);
    editLinkHiper.VisibleIndex = 6;

    GridColumn delLinkHiper = new GridColumn();
    delLinkHiper.Caption = "刪除";
    delLinkHiper.Visible = true;

    delLinkHiper.ColumnEdit = stationDel;
    gvStation.Columns.Add(delLinkHiper);
    delLinkHiper.VisibleIndex = 7;

    gvStation.BestFitColumns();

 

 

            以上數據已全部綁定完成

    3.觸發事件

            在上圖中點擊事件Click,新增不多說,彈出新窗體即可 

 

//編輯
    private void stationEdit_Click(object sender, EventArgs e)
    {
        int[] selectRows = gvStation.GetSelectedRows();
        //賦值
        int tsNo = Convert.ToInt32(gvStation.GetRowCellValue(selectRows[0], "TS_NO"));  //TS_NO是控件列名

        //具體操作因人而異

       ......
    }   

     //刪除
    private void stationDel_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("確定刪除所選數據?", "刪除提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {

    int[] selectRows = gvStation.GetSelectedRows();
    //賦值
    int  tsNO = Convert.ToInt32(gvStation.GetRowCellValue(selectRows[0], "TS_NO"));

           //寫sql語句執行刪除操作就可以了。
        }

    }

 

 
        

 

最后貼一張效果圖:


免責聲明!

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



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