⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagrid使用心得(附大量代码).txt

📁 .net常用经典源代码。十分好用!不用客气
💻 TXT
📖 第 1 页 / 共 2 页
字号:
DataGrid使用心得(附大量代码) 
http://dev.21tx.com 2006年05月08日 luoyixiaoniba 

 
 
 
 


每日文章精萃 
.Net:浅议C++/CLI的gcnew关键字 Java:如何打造实用网站 
ASP:Asp Object 之:AppendToLog PHP:浅谈Windows下 PHP4.0与oracle 8的连 
JSP:用缓冲技术提高JSP应用的性能和稳定 CGI:CGI教学:CGI安全问题 
VB:如何获得系统光驱的盘符 VC:创建DirectX环境 


 
   
 
DataGrid使用心得(附大量代码)

    1. 为 DataGrid 控件设计样式
    
    在<ASP:datagrid id="DataGrid1" runat="server">之后添加如下代码
    
    <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
    
    说明:
    (1) 在每个标签内主要是  ForeColor   BackColor  Font-Bold 这几个属性值
    
    2. 为 DataGrid 控件添加绑定列
    
    <asp:BoundColumn DataField="" ReadOnly="True" HeaderText=""></asp:BoundColumn>
    说明:
    (1) 在标签内的基本属性是 DataField / HeaderText 
    (2) DataFormatString 用于 获取或设置指定列中各项的显示格式的字符串。
     形式为 { A: Bxx }。例如,格式化字符串 {0:F2} 将显示带两位小数的定点数。
     其中A值只能设置为 0,因为每个单元格中只有一个值。
     冒号后的字符(常规示例中为 B)指定值的显示格式
     C  以货币格式显示数值。 
        D  以十进制格式显示数值。 
        E  以科学记数法(指数)格式显示数值。 
        F  以固定格式显示数值。 
        G  以常规格式显示数值。 
        N  以数字格式显示数值。 
        X  以十六进制格式显示数值。 
    (3) Visible 获取或设置一个值,该值指示此列在 DataGrid 控件中是否可见。
    (4) ReadOnly 设置些列是否只读,若是只读的话,则不能修改.
    (5) SortExpression 获取或设置选择进行排序的列时传递到 OnSortCommand 方法的字段或表达式的名称。
   

  3. 为 DataGrid 控件添加模板列
    <asp:TemplateColumn HeaderText="类别">
     <ItemTemplate>
      <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "actorclassname") %>' runat="server" ID="Label1"/>
     </ItemTemplate>
     <EditItemTemplate>
      <select name="sltclassname">
       <% = ActorClass.GetParentClass(0) %>
      </select>
     </EditItemTemplate>
    </asp:TemplateColumn>
    说明:
    (1) 基本框架是
      <asp:TemplateColumn HeaderText="类别">
    <ItemTemplate></ItemTemplate>
     </asp:TemplateColumn>
    (2) 全面的模板列
     <asp:TemplateColumn>

               <HeaderTemplate>
                  <b> Tax </b>
               </HeaderTemplate>

               <ItemTemplate>
                  <asp:Label
                       Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
                       runat="server"/>
               </ItemTemplate>

               <EditItemTemplate>

                  <asp:CheckBox
                       Text="Taxable" 
                       runat="server"/>

               </EditItemTemplate>

               <FooterTemplate>
                  <asp:HyPerlink id="HyperLink1"
                       Text="Microsoft"
                       NavigateUrl="http://www.microsoft.com"
                       runat="server"/>
               </FooterTemplate>

            </asp:TemplateColumn>
 (3) 为布尔型列应用模板列
  <asp:TemplateColumn>
         <ItemTemplate>
             <asp:Label
                  Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
                     runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
             <asp:CheckBox
                  Text="Taxable" 
                     runat="server"/>

            </EditItemTemplate>
        </asp:TemplateColumn>
        在正常状态,用 Label控件显示
        在编辑状态,用 CheckBox控件显示
    
 (4) 为枚举类型列应用模板列,如业务地区(全网/广东/云南等等)
  <asp:TemplateColumn HeaderText="处理方式">
   <ItemTemplate>
    <asp:Label ID="lbStatus">
     <%# DataBinder.Eval(Container, "DataItem.DealWith") %>
    </asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
    <asp:DropDownList id="dpStatus2" runat="server" DataTextField="status">
     <asp:ListItem Value="Log">Log(日志)</asp:ListItem>
     <asp:ListItem Value="SendSms">SendSms(短信)</asp:ListItem>
    </asp:DropDownList>
   </EditItemTemplate>
  </asp:TemplateColumn>
  在正常状态,用 Label控件显示
  在编辑状态,用 DropDownList控件显示
 
 (5) 为长字符串应用模板列,如一篇文章的内容
  还未做过
  
 4. 为 DataGrid 控件添加按钮列
 
 <asp:ButtonColumn 
        HeaderText="Remove from cart" 
        ButtonType="PushButton" 
        Text="Remove" 
        CommandName="RemoveFromCart" />
    (1) 要使用按钮列,必须在 DataGrid 控件中添加 OnItemCommand 属性,并为该事件添加处理方法.
    (2) 模板列可以实现按钮列能实现的任何功能.
    
    5. 为 DataGrid 控件添加编辑列
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
    (1) ButtonType 有两个值: LinkButton 超级链接样式按钮的列 | PushButton 普通按钮的列。 

 6. 为 DataGrid 控件添加超链接列
 <asp:HyperLinkColumn Text="添加子类" DataNavigateUrlField="ActorclassID" DataNavigateUrlFormatString="addActorClass.aspx?classID={0}"></asp:HyperLinkColumn>
 (1) 为每一行设置相同的文字及跳转的URL地址
  设置 Text 和 NavigateUrl 属性,则列中的所有超级链接将共享同一标题和 URL
 (2) 为每一行设置不同的文字及不同跳转的URL地址
  A.  用 DataTextField 设置数据源字段,若还想在原数据的基础上加工一下(如字段值为300,想显示为300元)
   则再设置 DataTextFormatString 字段
  B. 用DataNavigateUrlField 及 DataNavigateUrlFormatString 来设置URL地址
   用DataTextField = "money" DataTextFormatString = "{0}元"
  C.  举例
   DataNavigateUrlField="ActorclassID" DataNavigateUrlFormatString="addActorClass.aspx?classID={0}"
   
 7. 为 DataGrid 控件添加"编辑"代码
  在 DataGrid 标签中加入 
  OnUpdateCommand="DataGrid1_Update" OnCancelCommand="DataGrid1_Cancel" OnEditCommand="DataGrid1_Edit"代码
  在codeBehind页面加入如下代码
  ///响应编辑按钮
  public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e) 
  {
   DataGrid1.EditItemIndex = e.Item.ItemIndex;
   if (Request.QueryString.Get("classID") != null)
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
  }
   
  ///响应取消按钮
  public void DataGrid1_Cancel(Object sender, DataGridCommandEventArgs e) 
  {
   DataGrid1.EditItemIndex = -1;
   if (Request.QueryString.Get("classID") != null)
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));

  }
  
  ///响应更新按钮   
  public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e) 
  {
   TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
   string className = ClassNameText.Text;
   int classID = Int32.Parse((e.Item.Cells[0].Text).ToString());
   TextBox orderID2 = (TextBox)e.Item.Cells[5].Controls[0];
   int orderID = Int32.Parse(orderID2.Text);
   ActorClass.ModifyActorClass(className,classID,orderID);

   DataGrid1.EditItemIndex = -1;
   if (Request.QueryString.Get("classID") != null)
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
   else
    Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
  }
  说明
  (1) DataGrid 事件处理程序的格式
   MethodName(Object sender, DataGridCommandEventArgs e) 
  (2) 更新按钮的说明
   A. 获取编辑状态中的文本框
    TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
    string className = ClassNameText.Text;
   B. 获取编辑状态中的下拉列表框
    方法一
    int classID;
    classID = Int32.Parse(Request.Form.Get("sltclassname"));
    方法二
    DropDownList bbb = (DropDownList)e.Item.Cells[10].FindControl("dpStatus2");
    string ddpValue = bbb.SelectedValue
   C. 获取编辑状态中的复选框
    bool boolEnabled = ((CheckBox)e.Item.FindControl("chk_enabled")).Checked;
    String str2;
    if (boolEnabled)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -