DataGrid1

2013-09-2512:26:31

1.DataGrid页面

  <asp:DataGrid ID="msgInfo1" PageSize="8" AutoGenerateColumns="False" BorderWidth="0"
               CellPadding="0" CellSpacing="2" AllowPaging="True" Width="100%" DataKeyField="ID"
               runat="server" onpageindexchanged="msgInfo1_PageIndexChanged"
               onitemcommand="msgInfo1_ItemCommand" onitemdatabound="msgInfo1_ItemDataBound" >
         <ItemStyle HorizontalAlign="Center" CssClass="td1"></ItemStyle>
         <HeaderStyle HorizontalAlign="Center" BackColor="SkyBlue" ForeColor="White" CssClass="td2">
         </HeaderStyle>
         <Columns>
             <%--<asp:BoundColumn DataField="业务标题" HeaderText="标题">
             <ItemStyle Width="33%"></ItemStyle>
             </asp:BoundColumn>--%>
             <asp:TemplateColumn HeaderText="标题" ItemStyle-Width="33%">
               <ItemTemplate>
                   <asp:HyperLink ID="hy标题" runat="server"></asp:HyperLink>
               </ItemTemplate>
             </asp:TemplateColumn>

             <asp:BoundColumn DataField="业务表名" HeaderText="分类">
                         <ItemStyle Width="33%"></ItemStyle>
             </asp:BoundColumn>
              <asp:TemplateColumn HeaderText="操作">
                      <%--<ItemStyle Width="10%"></ItemStyle>--%>
                     <ItemTemplate>
                     <asp:Button ID="btnEdit" runat="server" Text="置顶" class="button" Width="100px" CommandName="toTop"
                                     CommandArgument='<%#Eval("ID") %>' />
                     <asp:Button ID="btnDelete" runat="server" Text="不公开" class="button" Width="100px"CommandName="toNoPublish" CommandArgument='<%#Eval("ID") %>' />
                      <%--<asp:ImageButton ID="btnModify" Runat="server" CommandName="ToEdit" AlternateText="点击进行编辑" ImageUrl="~/images/i_edit.gif"></asp:ImageButton>
                      <asp:ImageButton ID="btnDelete" Runat="server" CommandName="ToDelete" ToolTip="点击删除当前项" ImageUrl="~/images/i_delete.gif"></asp:ImageButton>--%>
                     </ItemTemplate>
                 </asp:TemplateColumn>
               </Columns>
        <PagerStyle Mode="NumericPages"></PagerStyle>
       </asp:DataGrid>

2.ItemDataBound实现a标签的链接,其中通过业务表名来进行类别筛选

  protected void msgInfo1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRow row = ((DataRowView)e.Item.DataItem).Row;
                HyperLink hy标题 = e.Item.FindControl("hy标题") as HyperLink;
                hy标题.Text = row["业务标题"].ToString();
                string 编号 = string.Empty;
                if (row["业务表名"].ToString() == "工作案例")
                {
                    编号 = "1";
                }
                else if (row["业务表名"].ToString() == "工作笔记")
                {
                    编号 = "4";
                }
                else if (row["业务表名"].ToString() == "实事工作")
                {
                    编号 = "3";
                }
                else if (row["业务表名"].ToString() == "义工服务")
                {
                    编号 = "7";
                }
                if (Comm.获取数据(row["业务表名"].ToString(), "ID", row["业务ID"].ToString(), "1").Rows.Count > 0)
                {
                    string 人员ID = Comm.获取数据(row["业务表名"].ToString(), "ID", row["业务ID"].ToString(), "1").Rows[0]["人员ID"].ToString();

                    hy标题.NavigateUrl = "javascript:aa('" + 人员ID + "','" + 编号 + "');";
                }
            }

3.js(aa()方法)

  <script>
         function aa(id, type) {
             //alert(id);
             window.open('qgy_ssgz.aspx?id=' + id + '&isView=1&OtherType=' + type, '', 'target=_blank,scrollbars=yes,top=100,left=200,width=800,height=700,scrolling=1');
         }
        </script>

4.ItemCommand实现DataGrid中的Button控件

  protected void msgInfo1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "toTop")
            {
               Publishcs.置顶已公布信息(e.CommandArgument.ToString());
               BindData(txt标题.Text.Trim());
            }
            else if (e.CommandName == "toNoPublish")
            {
                string 表名 = Comm.获取数据("已公布信息表","ID",e.CommandArgument.ToString(),"1").Rows[0]["业务表名"].ToString();
                Publishcs.不公布已公布信息(e.CommandArgument.ToString(), 表名,string.Empty);
                BindData(txt标题.Text.Trim());
            }         
        }

5.PageIndexChanged实现页面的切换

  protected void msgInfo1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            msgInfo1.CurrentPageIndex = e.NewPageIndex;          
            BindData("");
        }

6.实现绑定

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData("");
            }
        }
        protected void BindData(string 标题)
        {           
            msgInfo1.DataSource = Publishcs.获取已发布列表(标题);
            msgInfo1.DataBind();            
        }

7.本列中是通过存储过程来实现对数据的获取

  创建Publishcs.cs类,写获取已发布列表(标题).

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data;
  using System.Data.SqlClient;

  namespace Business
  {
      public class Publishcs
      {        
          /// <summary>
          /// 已发布类
          /// </summary>
          /// <returns></returns>
          private static SqlConnection GetConn()
          {
              SqlConnection conn = new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CNHRConnectionStrings"].ConnectionString);
              return conn;
          }


          public static DataTable 获取已发布列表(string 标题)
          {
              SqlConnection conn = GetConn();
              try
              {
                  conn.Open();
                  SqlCommand cmd = new SqlCommand("已发布列表获取", conn);
                  cmd.CommandType = CommandType.StoredProcedure;

                  SqlParameter para1 = new SqlParameter("@标题", SqlDbType.NVarChar);
                  para1.Value = 标题;
                  cmd.Parameters.Add(para1);

                  //SqlParameter para2 = new SqlParameter("@分类", SqlDbType.NVarChar);
                  //para2.Value = 分类;
                  //cmd.Parameters.Add(para2);

                  DataTable dt = new DataTable();
                  SqlDataAdapter sda = new SqlDataAdapter(cmd);
                  sda.Fill(dt);
                  return dt;
              }
             finally
              {
                  conn.Close();
              }
          }

          public static int 置顶已公布信息(string ID)
          {
              SqlConnection conn = GetConn();
              try
              {
                  conn.Open();
                  SqlCommand cmd = new SqlCommand("已公布信息置顶", conn);
                  cmd.CommandType = CommandType.StoredProcedure;

                  SqlParameter para1 = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                  para1.Value = new Guid(ID);
                  cmd.Parameters.Add(para1);
                
                  return cmd.ExecuteNonQuery();
              }
              finally
              {
                 conn.Close();
              }
          }

          public static int 不公布已公布信息(string 已公布信息ID, string 表名, string 业务ID)
          {
              SqlConnection conn = GetConn();
              try
              {
                  conn.Open();
                  SqlCommand cmd = new SqlCommand("将已公布信息变为不公布", conn);
                  cmd.CommandType = CommandType.StoredProcedure;

                  SqlParameter para1 = new SqlParameter("@已公布信息ID", SqlDbType.UniqueIdentifier);
                  para1.Value = string.IsNullOrEmpty(已公布信息ID) ? Guid.Empty : new Guid(已公布信息ID);
                  cmd.Parameters.Add(para1);

                  SqlParameter para2 = new SqlParameter("@表名", SqlDbType.NVarChar);
                  para2.Value = 表名;
                  cmd.Parameters.Add(para2);

                  SqlParameter para3 = new SqlParameter("@业务ID", SqlDbType.UniqueIdentifier);
                  para3.Value = string.IsNullOrEmpty(业务ID) ? Guid.Empty : new Guid(业务ID);
                  cmd.Parameters.Add(para3);

                  return cmd.ExecuteNonQuery();
              }
              catch (Exception ex)
              {
                  throw new Exception(ex.Message);
              }
              finally
              {
                  conn.Close();
              }
          }
      }
  }

原文地址:https://www.cnblogs.com/kim0zh/p/2013-09-25DataGrid.html