现在的位置: 首页 > 综合 > 正文

DataGrid的分页、行变色、升降序、删除、编辑

2012年08月16日 ⁄ 综合 ⁄ 共 6287字 ⁄ 字号 评论关闭
        private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!this.IsPostBack)
            
{
                
this.DataBindtoDataGrid();
            }

            
// 在此处放置用户代码以初始化页面
        }

        
private void DataBindtoDataGrid()
        
{
            SqlConnection con
=DB.createCon();
            con.Open();
            SqlDataAdapter sda
=new SqlDataAdapter();
            sda.SelectCommand
=new SqlCommand("select userID,userName,userIdentity,userBirthday from loginnew",con);
            DataSet ds
=new DataSet();
            sda.Fill(ds,
"loginnew");
            
this.DataGrid1.DataKeyField="userID";//取出的主键存储在DataKeys中
            this.DataGrid1.DataSource=ds.Tables["loginnew"];
            
this.DataGrid1.DataBind();

        }


        
Web 窗体设计器生成的代码


        
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)//用于分页
        {
            
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
            
this.DataBindtoDataGrid();
        }


        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
this.DataGrid1.Columns[1].Visible=false;
            
this.DataBindtoDataGrid();
        }


        
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)//每一行绑定的时候都会激发这个事件
        {
            
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)//e.Item.ItemType鼠标当前位置的模版类型,
            {
                e.Item.Attributes.Add(
"onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");//e.Item.Attributes.Add()可以用于给这一Item添加事件
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
                e.Item.Cells[
6].Attributes.Add("onclick","return confirm('你确认删除吗?');");
                
                
            }

        }


        
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        
{
            
if(ViewState["Order"]==null)
            
{
                ViewState[
"Order"]="ASC";//升序
            }

            
else
            
{
                
if(ViewState["Order"].ToString()=="ASC")
                
{
                    ViewState[
"Order"]="DESC";
                }

                
else
                
{
                    ViewState[
"Order"]="ASC";
                }

            }

            SqlConnection con
=DB.createCon();
            con.Open();
            SqlDataAdapter sda
=new SqlDataAdapter();
            sda.SelectCommand
=new SqlCommand("select userID,userName,userIdentity,userBirthday from loginnew",con);
            DataSet ds
=new DataSet();
            sda.Fill(ds,
"loginnew");
            ds.Tables[
"loginnew"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString();
            
this.DataGrid1.DataSource=ds.Tables["loginnew"].DefaultView;
            
this.DataGrid1.DataBind();
        }


        
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
string userID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
            SqlConnection con
=DB.createCon();
            con.Open();
            SqlCommand cmd
=new SqlCommand("delete from loginnew where userID='"+userID+"'",con);
            cmd.ExecuteNonQuery();
            
            
this.DataBindtoDataGrid();

        }


        
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
            
this.DataBindtoDataGrid();
        }


        
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
this.DataGrid1.EditItemIndex=-1;//因为index都是大于0的,-1就是没有
            this.DataBindtoDataGrid();
        }


        
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
            
string userID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
            
string userName=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
            Response.Write(userID
+"&"+userName);
            
this.DataGrid1.EditItemIndex=-1;
            
this.DataBindtoDataGrid();

        }


        
private void Button2_Click(object sender, System.EventArgs e)
        
{
            
foreach(System.Web.UI.WebControls.DataGridItem dl in this.DataGrid1.Items)
            
{
                CheckBox chk
=(CheckBox)dl.FindControl("chkSelect");
                
if(chk.Checked)
                
{
                    Response.Write(dl.Cells[
1].Text);
                }

            }

        }

抱歉!评论已关闭.