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( '你确认删除 +eRowCells[]Text+ 吗? ') ); //鼠标经过时改变行的颜色 eRowAttributesAdd( onmouseover thisstylebackgroundColor= '#ffffe ' ); eRowAttributesAdd( onmouseout thisstylebackgroundColor= 'transparent ' ); } } //执行一条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= ' + name + 'Status= ' + status + 'Addr= ' + addr + 'Addr= ' + addr + 'City= ' + city + 'State= ' + state + 'Zip= ' + zip + ' 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 ' + name + ' ' + status + ' ' + addr + ' ' + addr + ' ' + city + ' ' + state + ' ' + zip + ') ; 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( ' + str_Message + '); </script > ); } //弹出确认对话框 public void Confirm(string str_Message string btn Page page) { pageRegisterStartupScript( <script > if (confirm( ' + str_Message + ')==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(); } } |