熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

DataGridView的常用用法

2013-11-13 09:50:17  來源: .NET編程 

  DataGridView的常用用法 //讀取表Supplier並綁定到GridView中
    private void BindGvSupplier()
    {
    OracleConnection conn = thisConn();
    OracleCommand cmd = new OracleCommand( select * from Supplier conn);
    OracleDataAdapter sda = new OracleDataAdapter(cmd);
    DataSet ds = new DataSet();
    sdaFill(ds Supplier );

  string[] SuppId = new string[dsTables[ Supplier ]RowsCount];
    for (int i = ; i < dsTables[ Supplier ]RowsCount; i++)
    {
    SuppId[i] = dsTables[ Supplier ]Rows[i][]ToString();
    }
    thisgvSupplierDataSource = dsTables[ Supplier ];
    thisgvSupplierDataKeyNames[] = SuppId ;
    thisgvSupplierDataBind();
    thislblPageIndexText = ConvertToString(intParse(thisgvSupplierPageIndexToString()) + );
    thislblTotalPageText = thisgvSupplierPageCountToString();
    thislblCountText = thisGetTotalCount()ToString();
    }

  //分頁觸發的事件
    protected void gvSupplier_PageIndexChanging(object sender GridViewPageEventArgs e)
    {
    thisgvSupplierPageIndex = eNewPageIndex;
    thisBindGvSupplier();
    thislblPageIndexText = ConvertToString(intParse(thisgvSupplierPageIndexToString()) + );
    thiscbAllSelect_CheckedChanged(thiscbAllSelect e);
    }

  //刪除按鈕觸發的事件
    protected void gvSupplier_RowDeleting(object sender GridViewDeleteEventArgs e)
    {
    int id = eRowIndex;

  GridViewRow gvr = thisgvSupplierRows[id];
    int SuppId=intParse(((HyperLink)(gvrCells[]Controls[]))TextToString());
    string sqlString = delete from Supplier where SuppId= + SuppId;
    //如果本頁只有一條數據刪除後要向前翻一頁
    if (thisgvSupplierRowsCount == )
    {
    if (thisgvSupplierPageIndex > )
    {
    thisgvSupplierPageIndex;
    }
    }
    int result = ExecuteSql(sqlString);
    if (result == )
    {
    thisAlert( 你成功刪除一條數據 thisPage);
    }
    thisBindGvSupplier();
    thisBindGvSupplier();

  }

  //綁定刪除按鈕的確認提示
    protected void gvSupplier_RowDataBound(object sender GridViewRowEventArgs e)
    {
    if (eRowRowType == DataControlRowTypeDataRow)
    {
    LinkButton myLb = (LinkButton)(eRowCells[]Controls[]);
    myLbAttributesAdd( onclick javascript:return confirm( &apos;你確認刪除 +eRowCells[]Text+ 嗎? &apos;) );
    //鼠標經過時改變行的顏色
    eRowAttributesAdd( onmouseover thisstylebackgroundColor= &apos;#ffffe &apos; );
    eRowAttributesAdd( onmouseout thisstylebackgroundColor= &apos;transparent &apos; );
    }
    }

  //執行一條Oracle語句
    private int ExecuteSql(String sqlString)
    {
    //try
    //{
    OracleConnection conn = thisConn();
    connOpen();
    OracleCommand cmd = new OracleCommand(sqlString conn);
    int effectedLine = cmdExecuteNonQuery();
    connClose();
    return effectedLine;
    //}
    //catch
    //{
    // return ;
    //}
    }


    //點擊編輯按鈕時觸發的事件
    protected void gvSupplier_RowEditing(object sender GridViewEditEventArgs e)
    {
    string s = thisgvSupplierDataKeys[eNewEditIndex][]ToString();
    thistxtAddrText = dddd + s;
    thisgvSupplierEditIndex = eNewEditIndex;
    thisBindGvSupplier();
    }
    //點擊取消按鈕時觸發的事件
    protected void gvSupplier_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)
    {
    thisgvSupplierEditIndex = ;
    thisBindGvSupplier();
    }
    //點擊更新按鈕時觸發的事件
    protected void gvSupplier_RowUpdating(object sender GridViewUpdateEventArgs e)
    {
    int id = eRowIndex;
    GridViewRow gvr = thisgvSupplierRows[id];
    int suppId = intParse(((HyperLink)(gvrCells[]Controls[]))TextToString());
    string name = ((TextBox)gvrCells[]Controls[])TextToString();
    string status = ((TextBox)gvrCells[]Controls[])TextToString();
    string addr = ((TextBox)gvrCells[]Controls[])TextToString();
    string addr =((TextBox) gvrCells[]Controls[])TextToString();
    string city = ((TextBox)gvrCells[]Controls[])TextToString();
    string state = ((TextBox)gvrCells[]Controls[])TextToString();
    string zip=((TextBox)gvrCells[]Controls[])TextToString();
    string sqlString = update Supplier set Name= &apos; + name + &apos;Status= &apos; + status + &apos;Addr= &apos; + addr + &apos;Addr= &apos; + addr + &apos;City= &apos; + city + &apos;State= &apos; + state + &apos;Zip= &apos; + zip + &apos; where SuppId= + suppId;
    int result = ExecuteSql(sqlString);
    if (result == )
    {
    //
    }
    thisgvSupplierEditIndex = ;
    thisBindGvSupplier();
    }

  //增加一條記錄
    protected void btnAdd_Click(object sender EventArgs e)
    {
    string name = thistxtNameTextToString();
    string status = thistxtStatusTextToString();
    string addr = thistxtAddrTextToString();
    string addr = thistxtAddrTextToString();
    string city = thistxtCityTextToString();
    string state = thistxtStateTextToString();
    string zip = thistxtZipTextToString();
    string sqlString = insert into Supplier values(SQSuppIdNextval &apos; + name + &apos; &apos; + status + &apos; &apos; + addr + &apos; &apos; + addr + &apos; &apos; + city + &apos; &apos; + state + &apos; &apos; + zip + &apos;) ;
    if (thisExecuteSql(sqlString) == )
    {
    thisAlert( 你成功添加一條數據 thisPage);
    }
    else
    {
    thisAlert( 添加未成功! thisPage);
    }
    thisBindGvSupplier();
    }

  //查看共有多少條數據
    private int GetTotalCount()
    {
    //try
    //{
    OracleConnection conn = thisConn();
    connOpen();
    OracleCommand cmd = new OracleCommand( select count(*) from Supplier conn);
    int count = ConvertToInt(cmdExecuteScalar());
    connClose();
    return count;
    //}
    //catch
    //{
    // return ;
    //}
    }

  //彈出警告窗口
    public void Alert(string str_Message Page page)
    {
    pageRegisterStartupScript( <script >alert( &apos; + str_Message + &apos;); </script > );
    }

  //彈出確認對話框
    public void Confirm(string str_Message string btn Page page)
    {
    pageRegisterStartupScript( <script > if (confirm( &apos; + str_Message + &apos;)==true){documentforms() + btn + click();} </script > );
    }

  protected void cbAllSelect_CheckedChanged(object sender EventArgs e)
    {
    for(int i=;i <thisgvSupplierRowsCount;i++)
    {
    CheckBox cbSelect=(CheckBox)(gvSupplierRows[i]Cells[]FindControl( cbSelect ));
    cbSelectChecked=thiscbAllSelectChecked;
    }
    }

  //當點擊刪除按鈕時刪除所有checkbox被選中的數據
    protected void btnDel_Click(object sender EventArgs e)
    {
    //如果用戶確認將觸發btnRealDel的事件
    thisConfirm( 你真的要刪除你所選的數據麼? btnRealDel thisPage);

  }

  //真實的刪除操作
    public void btnRealDel_Click(object sender EventArgs e)
    {
    int count = ;
    for (int i = ; i < thisgvSupplierRowsCount; i++)
    {
    CheckBox myCb = (CheckBox)(thisgvSupplierRows[i]Cells[]FindControl( cbSelect ));
    if (myCbChecked)
    {
    count++;
    HyperLink hl = (HyperLink)(thisgvSupplierRows[i]Cells[]Controls[]);
    int suppId = intParse(hlTextToString());
    string sqlString = delete from Supplier where SuppId= + suppId;
    thisExecuteSql(sqlString);
    }
    }
    if (count > )
    {
    thisAlert( 你成功刪除了 + count + 條數據 thisPage);
    thisBindGvSupplier();
    }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11706.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.