DataGridView的常用用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataGridView的常用用法相关的知识,希望对你有一定的参考价值。
参考技术ADataGridView的常用用法 //读取表Supplier并绑定到GridView中 private void BindGvSupplier() OracleConnection conn = this Conn(); OracleCommand cmd = new OracleCommand( select * from Supplier conn); OracleDataAdapter sda = new OracleDataAdapter(cmd); DataSet ds = new DataSet(); sda Fill(ds Supplier );
string[] SuppId = new string[ds Tables[ Supplier ] Rows Count]; for (int i = ; i < ds Tables[ Supplier ] Rows Count; i++) SuppId[i] = ds Tables[ Supplier ] Rows[i][ ] ToString(); this gvSupplier DataSource = ds Tables[ Supplier ]; this gvSupplier DataKeyNames[ ] = SuppId ; this gvSupplier DataBind(); this lblPageIndex Text = Convert ToString(int Parse(this gvSupplier PageIndex ToString()) + ); this lblTotalPage Text = this gvSupplier PageCount ToString(); this lblCount Text = this GetTotalCount() ToString();
//分页触发的事件 protected void gvSupplier_PageIndexChanging(object sender GridViewPageEventArgs e) this gvSupplier PageIndex = e NewPageIndex; this BindGvSupplier(); this lblPageIndex Text = Convert ToString(int Parse(this gvSupplier PageIndex ToString()) + ); this cbAllSelect_CheckedChanged(this cbAllSelect e);
//删除按钮触发的事件 protected void gvSupplier_RowDeleting(object sender GridViewDeleteEventArgs e) int id = e RowIndex;
GridViewRow gvr = this gvSupplier Rows[id]; int SuppId=int Parse(((HyperLink)(gvr Cells[ ] Controls[ ])) Text ToString()); string sqlString = delete from Supplier where SuppId= + SuppId; //如果本页只有一条数据 删除后要向前翻一页 if (this gvSupplier Rows Count == ) if (this gvSupplier PageIndex > ) this gvSupplier PageIndex ; int result = ExecuteSql(sqlString); if (result == ) this Alert( 你成功删除一条数据 this Page); this BindGvSupplier(); this BindGvSupplier();
//绑定删除按钮的确认提示 protected void gvSupplier_RowDataBound(object sender GridViewRowEventArgs e) if (e Row RowType == DataControlRowType DataRow) LinkButton myLb = (LinkButton)(e Row Cells[ ] Controls[ ]); myLb Attributes Add( onclick javascript:return confirm( '你确认删除 +e Row Cells[ ] Text+ 吗? ') ); //鼠标经过时改变行的颜色 e Row Attributes Add( onmouseover this style backgroundColor= '#ffffe ' ); e Row Attributes Add( onmouseout this style backgroundColor= 'transparent ' );
//执行一条Oracle语句 private int ExecuteSql(String sqlString) //try // OracleConnection conn = this Conn(); conn Open(); OracleCommand cmd = new OracleCommand(sqlString conn); int effectedLine = cmd ExecuteNonQuery(); conn Close(); return effectedLine; // //catch // // return ; //
//点击编辑按钮时触发的事件 protected void gvSupplier_RowEditing(object sender GridViewEditEventArgs e) string s = this gvSupplier DataKeys[e NewEditIndex][ ] ToString(); this txtAddr Text = dddd + s; this gvSupplier EditIndex = e NewEditIndex; this BindGvSupplier(); //点击取消按钮时触发的事件 protected void gvSupplier_RowCancelingEdit(object sender GridViewCancelEditEventArgs e) this gvSupplier EditIndex = ; this BindGvSupplier(); //点击更新按钮时触发的事件 protected void gvSupplier_RowUpdating(object sender GridViewUpdateEventArgs e) int id = e RowIndex; GridViewRow gvr = this gvSupplier Rows[id]; int suppId = int Parse(((HyperLink)(gvr Cells[ ] Controls[ ])) Text ToString()); string name = ((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); string status = ((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); string addr = ((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); string addr =((TextBox) gvr Cells[ ] Controls[ ]) Text ToString(); string city = ((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); string state = ((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); string zip=((TextBox)gvr Cells[ ] Controls[ ]) Text ToString(); 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 == ) // this gvSupplier EditIndex = ; this BindGvSupplier();
//增加一条记录 protected void btnAdd_Click(object sender EventArgs e) string name = this txtName Text ToString(); string status = this txtStatus Text ToString(); string addr = this txtAddr Text ToString(); string addr = this txtAddr Text ToString(); string city = this txtCity Text ToString(); string state = this txtState Text ToString(); string zip = this txtZip Text ToString(); string sqlString = insert into Supplier values(SQSuppId Nextval ' + name + ' ' + status + ' ' + addr + ' ' + addr + ' ' + city + ' ' + state + ' ' + zip + ') ; if (this ExecuteSql(sqlString) == ) this Alert( 你成功添加一条数据 this Page); else this Alert( 添加未成功! this Page); this BindGvSupplier();
//查看共有多少条数据 private int GetTotalCount() //try // OracleConnection conn = this Conn(); conn Open(); OracleCommand cmd = new OracleCommand( select count(*) from Supplier conn); int count = Convert ToInt (cmd ExecuteScalar()); conn Close(); return count; // //catch // // return ; //
//弹出警告窗口 public void Alert(string str_Message Page page) page RegisterStartupScript( <script >alert( ' + str_Message + '); </script > );
//弹出确认对话框 public void Confirm(string str_Message string btn Page page) page RegisterStartupScript( <script > if (confirm( ' + str_Message + ')==true)document forms( ) + btn + click(); </script > );
protected void cbAllSelect_CheckedChanged(object sender EventArgs e) for(int i= ;i <this gvSupplier Rows Count;i++) CheckBox cbSelect=(CheckBox)(gvSupplier Rows[i] Cells[ ] FindControl( cbSelect )); cbSelect Checked=this cbAllSelect Checked;
//当点击删除按钮时删除所有checkbox被选中的数据 protected void btnDel_Click(object sender EventArgs e) //如果用户确认将触发btnRealDel的事件 this Confirm( 你真的要删除你所选的数据么? btnRealDel this Page);
lishixinzhi/Article/program/net/201311/11706
新手向:DataGridView常见用法及问题
我是做WinForm开发的.对于WinForm开发来说,DataGridview这个控件是再熟悉不过了.使用频率超高.
在刚做WinForm开发时,由于不熟悉这个控件也曾遇到不少麻烦.如今觉得自己掌握了,就把一些常见用法和问题列出来供新入门的朋友参考.
1.DataGridview绑定数据前的属性设置小问题
(1)如果在事先已经明确要展示的表结构,可以通过 右键->编辑列 进入编辑控件界面显示的成员
如图可见不特意改某些属性的情况下,我们的Datagridview生成的界面是有一列空白列和一行空白行,并且每个格子都是可以点击修改的.
就我个人而言,基本遇到的情况是要设置Datagridview去掉空白行空白列并且设置成每次点击都选取一行而不只选某一列.
为达成这个目的,我们需要设置:
控件属性:AllowUserToAddRows=false;//去掉最后一行空白行.
控件属性:RowHeadersVisible=false;//去掉第一列空白列.
控件属性:SelectionMode=FullRowSelect;//每次选中都选中整行
编辑列属性:ReadOnly=true;使某列只读(一般展示性的Datagridview最好在创建列时就设置列只读或在代码中遍历每一个列设置只读属性=true或者直接设置Datagridview控件属性ReadOnly=true)
完成效果:
可以看到列标题是可以点击的,可以用于排序,箭头表示顺序还是逆序.
个人认为这是十分便利的功能,但不排除有需要让列标题不可点击的情况.
这是需要的设置的是:控件编辑列->SortMode=NotSortTable
如图
如此此列就无法点击排序.
至此是在绑定数据前的小问题.超新手向.那下一节将讲述Datagridview的绑定数据源问题.
以上是关于DataGridView的常用用法的主要内容,如果未能解决你的问题,请参考以下文章
C# .net不同版本中winform控件 dataGrid /DataGrid/DataGridView 有啥区别