關於GridEx的操作(一)GridEx的數據操作

1.綁定數據;

仍然以prodcutDataSet的products表為例.

新建grdEXProduct;按照(一)設置GridEx樣式;

grdEXProduct.DataSource=productDataset;Datamenber=products;

sqlString="Select * from products";

DBHelper.ExecuteDataSet(SqlString, dataSetSuppliers1, "products");//DBHelper是我自己參考網上的例子寫的一個數據操作類;

//支持MsSql,MYSql,OleDb,odbc,Oracle 等;

 

2.新增:

datarow row= productDataset.products.Rows.newrow();

row["ProductKey"]="-1";

row["ProductID"]="sss";

productDataset.products.Rows.addRow(row);

3:刪除:

datarow row=productDataset.products.Rows[Gridex1.getRow().rowindex];

productDataset.products.Rows.remove(row)

4.保存數據

grdEXProduct.UpdateData();//很重要;否則,你輸入的東西不能保存)

foreach (DataRow row in productDataset.products.Rows)

 {

      if (row.RowState == DataRowState.Modified)
      {
             SqlString = " Update products set products.supplygoodsKey= " + row["supplygoodsKey"].ToString()
                            + ",products.SupplierKey=" + row["SupplierKey "].ToString()

                            + ",price=" + row["price"].ToString()
                            + " Where products.ProductKey=" +row["ProductKey"].ToString();
        }//修改更新
        else
       {
               SqlString = " Insert into products (supplygoodsKey,supplierKey,price) values("
                           + row["supplygoodsKey"].ToString() + "," + row["SupplierKey "].ToString()

                           + ", " + row["price"].ToString() + ");";

         }//新增

  DBHelper.ExecuteNonQuery(SqlString);

 

}

5.獲得運行時的dropwodn數據  public enum myFlag{ EditFlag = 1,SaveFlag = 2, AddFlag = 3,DelFlag = 4 }//狀態標致

private int ii = 0;

 private void grdEXProduct_DropDown(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            if (grdEXProduct.GetRow() != null)
            {
                if (mflag != myFlag.SaveFlag)
                {
                    DataRow row = dataSetOutStore1.OutStoreSubItem.Rows[grdEXProduct.GetRow().RowIndex];
                    DataSet ds = new DataSet();
                    SqlString = "select distinct suppliers.SupplierName from suppliers "
                            + " inner join supplyGoods on supplygoods.SupplierKey=suppliers.SupplierKey"
                            + " inner join outstoresubitem on outstoresubitem.ProductKey=supplyGoods.ProductKey "
                            + " where outstoresubitem.ProductKey=" + row["productKey"].ToString() + ";";
                    ds = DBHelper.ExecuteDataSet(SqlString, "suppliers");
                    int ii = 0;
                    Janus.Windows.GridEX.GridEXValueListItemCollection item = new 

                   Janus.Windows.GridEX.GridEXValueListItemCollection();//設定ValueListItemCollection
                    foreach (DataRow ro in ds.Tables["Suppliers"].Rows)
                    {
                        item.Add(ii, ro["SupplierName"].ToString());
                        ii++;
                    }
                    grdEXProduct.RootTable.Columns["SupplierName"].EditValueList = item;//editvaluelist的值,不是valuelist噢

                }
            }
        }

 

 private void grdEXProduct_CellValueChanged(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            if (e.Column.EditValueList != null)
            {

                int ii = 0;
                grdEXProduct.UpdateData();
                DataRow row = dataSetOutStore1.OutStoreSubItem.Rows[grdEXProduct.GetRow().RowIndex];
                ii = Convert.ToInt32(row["SupplierName"]);//row["SupplierName"]是editValuelist的值,你更新數據後,

 

              //girdex的cell顯示的是editValuelist的序號

                DataSet ds = new DataSet();
                SqlString = "select distinct supplyGoods.supplyGoodsKey from supplyGoods "
                        + " inner join suppliers ON suppliers.SupplierKey=supplyGoods.SupplierKey"
                        + " inner join products on products.ProductKey=supplyGoods.ProductKey "
                        + " where supplyGoods.ProductKey=" + row["productKey"].ToString()
                        + " and suppliers.supplierName='" + e.Column.EditValueList[ii].Text + "';";
                ds = DBHelper.ExecuteDataSet(SqlString, "supplyGoods");
                if (ds.Tables["supplyGoods"].Rows.Count > 0)
                {
                    DataRow rr = ds.Tables["supplyGoods"].Rows[0];
                    row["supplyGoodsKey"] = rr["supplyGoodsKey"];
                    row["SupplierName"] = e.Column.EditValueList[ii].Text;
                }
            }
        }

 

6.動態設定gridex的樣式

  foreach (DataRow row inproductDataset.products.Rows)
   {
                   if (row["flag"].ToString() == true.ToString() || row["flag"].ToString()=="1")
                   {
                       grdEXProduct.GetRow(row).RowStyle = new Janus.Windows.GridEX.GridEXFormatStyle();
                      grdEXProduct.GetRow(row).RowStyle.ForeColor = Color.Red;
                       grdEXProduct.GetRow(row).RowStyle.FontStrikeout = Janus.Windows.GridEX.TriState.True;
                   }
    }

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页