donet学习笔记8(ADO基础B:DataGird控件)

1.界面:
员工编号员工编号员工生日标题  删除
111948年12月8日 Sales RepresentativeNancyDavolio删除编辑
221952年2月19日 Vice President, SalesAndrewFuller删除编辑
331963年8月30日 Sales RepresentativeJanetLeverling删除编辑
         
上一页 下一页



2.数据库还是用的微软提供的本身NORTHWIND库.本界面用datagird控件实现了 分页,按照用户定制要求 排序,.对某个栏位的 隐藏,删除,编辑功能.遍历控件cell实现批量删除
可以用手动的配置或则是数据库的自动的配置.其实一般都没有在代码上写,都是通过可视话界面进行改动
ASP代码如下:
  < asp:DataGrid  ID ="DataGrid1"  runat ="server"  Style ="z-index: 100; left: 26px; position: absolute;
            top: 28px"
 BackColor ="LightGoldenrodYellow"  BorderColor ="Tan"  BorderWidth ="1px"  CellPadding ="2"  ForeColor ="Black"  GridLines ="None"  AllowPaging ="True"  AutoGenerateColumns ="False"  OnPageIndexChanged ="DataGrid1_PageIndexChanged"  PageSize ="3"  ShowFooter ="True"  OnItemDataBound ="DataGrid1_ItemDataBound"  AllowSorting ="True"  OnDeleteCommand ="DataGrid1_DeleteCommand"  OnSortCommand ="DataGrid1_SortCommand"  OnCancelCommand ="DataGrid1_CancelCommand"  OnEditCommand ="DataGrid1_EditCommand"  OnUpdateCommand ="DataGrid1_UpdateCommand" >
            
< FooterStyle  BackColor ="Tan"   />
            
< SelectedItemStyle  BackColor ="DarkSlateBlue"  ForeColor ="GhostWhite"   />
            
< PagerStyle  BackColor ="PaleGoldenrod"  ForeColor ="DarkSlateBlue"  HorizontalAlign ="Center"  NextPageText ="下一页"  PrevPageText ="上一页"   />
            
< AlternatingItemStyle  BackColor ="PaleGoldenrod"   />
            
< HeaderStyle  BackColor ="Tan"  Font-Bold ="True"   />
            
< Columns >
                
< asp:HyperLinkColumn  DataNavigateUrlField ="employeeID"  DataNavigateUrlFormatString ="showDetails.aspx?empID={0}"
                    DataTextField
="employeeID"  HeaderText ="员工编号"  Target ="_blank" ></ asp:HyperLinkColumn >
                
< asp:BoundColumn  DataField ="employeeID"  HeaderText ="员工编号"  ReadOnly ="True" ></ asp:BoundColumn >
                
< asp:TemplateColumn  HeaderText ="员工生日" >
                    
< ItemTemplate >
                        
< asp:Label  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") % > '> </ asp:Label >
                   
</ ItemTemplate >
                    
< EditItemTemplate >
                        
< asp:TextBox  ID ="TextBox1"  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") % > '> </ asp:TextBox >
                        
< asp:CompareValidator  ID ="CompareValidator1"  runat ="server"  Display ="Dynamic"  ErrorMessage ="日期不合法"
                             Type
="Date"  ControlToValidate ="TextBox1"  Operator ="DataTypeCheck" ></ asp:CompareValidator >
                    
</ EditItemTemplate >
                
</ asp:TemplateColumn >
                
< asp:BoundColumn  DataField ="Title"  HeaderText ="标题" ></ asp:BoundColumn >
                
< asp:BoundColumn  DataField ="FirstName"  HeaderText ="名" ></ asp:BoundColumn >
                
< asp:BoundColumn  DataField ="LastName"  HeaderText ="姓" ></ asp:BoundColumn >
                
< asp:ButtonColumn  CommandName ="Delete"  Text ="删除" ></ asp:ButtonColumn >
                
< asp:TemplateColumn >
                    
< ItemTemplate >
                        
< asp:LinkButton  runat ="server"  CausesValidation ="false"  CommandName ="Edit"    ID ="Edit"   Text ="编辑" ></ asp:LinkButton >
                    
</ ItemTemplate >
                    
< EditItemTemplate >
                        
< asp:LinkButton  runat ="server"  CommandName ="Update"  Text ="更新" ></ asp:LinkButton >
                        
< asp:LinkButton  runat ="server"  CausesValidation ="false"  CommandName ="Cancel"  Text ="取消" ></ asp:LinkButton >
                    
</ EditItemTemplate >
                
</ asp:TemplateColumn >
                
< asp:TemplateColumn  HeaderText ="删除" >
                    
< ItemTemplate >
                        
< asp:CheckBox  ID ="CheckBoxSelect"  runat ="server"    />
                    
</ ItemTemplate >
                
</ asp:TemplateColumn >
            
</ Columns >
        
</ asp:DataGrid >

3.后台代码:
using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;

public  partial  class  DataGridTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindToDataGird();
        }

        
    }

    
private void BindToDataGird()
    
{
        SqlConnection conn 
= DB.getConnection();
        SqlDataAdapter sda 
= new SqlDataAdapter();
        sda.SelectCommand 
= new SqlCommand("select * from employees", conn);
        DataSet ds 
= new DataSet();
        sda.Fill(ds, 
"emp");
        DataGrid1.DataKeyField 
= "employeeID";

        DataGrid1.DataSource 
= ds.Tables["emp"];
        DataGrid1.DataBind();
       
    }

    
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    
{
        DataGrid1.CurrentPageIndex 
= e.NewPageIndex;
        BindToDataGird();
    }


    
protected void ButtonHide_Click(object sender, EventArgs e)
    
{
        
//直接把第二项的可见性设置为FALSE
        DataGrid1.Columns[1].Visible = false;
        BindToDataGird();
    }

    
//当数据每一项绑定的时候
    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    
{
        
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        
{
            
//小技巧,实际上是本地JS脚本事件
            e.Item.Attributes.Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#559944'");
            e.Item.Attributes.Add(
"onmouseout""this.style.backgroundColor=c");
            
//Cell表示是单元格Controls表示里边的控件
            ((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('你确认删除???');");
        }

        

    }

    
protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
    
{
        
//ViewState存储在客户端.
        if (ViewState["Order"== null)
        
{
            ViewState[
"Order"="ASC";
        }

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

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

        }

        
        
//数据绑定显示
        SqlConnection conn = DB.getConnection();
        SqlDataAdapter sda 
= new SqlDataAdapter();
        sda.SelectCommand 
= new SqlCommand("select * from employees", conn);
        DataSet ds 
= new DataSet();
        sda.Fill(ds, 
"emp");
        ds.Tables[
"emp"].DefaultView.Sort = e.SortExpression+" " +ViewState["Order"].ToString();
        DataGrid1.DataSource 
= ds.Tables["emp"].DefaultView;
        DataGrid1.DataBind();
        
    }

    
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    
{
        
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
        SqlConnection conn 
= DB.getConnection();
        SqlCommand cmd 
= new SqlCommand("delete from employees where employeeID = '" + empID + "'", conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        BindToDataGird();
    }

    
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    
{
        DataGrid1.EditItemIndex 
= e.Item.ItemIndex;
        BindToDataGird();
    }

    
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    
{
        DataGrid1.EditItemIndex 
= -1;
        BindToDataGird();
    }

    
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    
{
        
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
      
//这个就是取的值的方法.
          string lastName = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
      
//之后连接数据库之后UPDATE就可以了!不再写了!
        
    }

    
protected void ButtonDel_Click(object sender, EventArgs e)
    
{
        
//遍历DataGrid1所有行
        foreach (System.Web.UI.WebControls.DataGridItem dll in DataGrid1.Items)
        
{
            
//找到名为"CheckBoxSelect"的控件
            CheckBox chk = (CheckBox)dll.FindControl("CheckBoxSelect");
            
if (chk.Checked)
            
{
                Response.Write(dll.Cells[
1].Text);
            }

        }

    }

}

转载于:https://www.cnblogs.com/sorry208/articles/930482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值