在ASP.Net和C#中处理多个GridView

时间:2017-07-06 06:00:23

标签: c# asp.net gridview

我有一个表列名为Status,它取三个值1,2或3.现在,我想在三个GridView中的任何一个中显示另一个列Name1,具体取决于Status。此外,我还有链接按钮,可以重定向到不同的Web表单。

这是我获得的输出。 GridView的ID是GridView1

enter image description here

这是我使用的代码

标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title></title>  
    <style type="text/css">  
        body{  
            font-family:Arial;  
            font-size:10px;  
        }  

        td,th{  

            height:25px;  
            width:100Px;  
        }  
    </style>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
    <hr />  
    <asp:GridView ID="GridView" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"  
            RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"  
            AutoGenerateColumns="false" Height="225px" Width="368px" 
            >  
         <Columns>  
                <asp:BoundField DataField="Name1" HeaderText="File Name" /> 
                <asp:BoundField DataField="Status" HeaderText="Status" />  
                 <asp:TemplateField ItemStyle-HorizontalAlign="Center">  
                    <ItemTemplate>  
                        <asp:LinkButton ID="lnkEdit" runat="server" HeaderText="Edit Status" Text="Edit Application" OnClick="EditFile"  
                            CommandArgument='<%# Eval("Status") %>'></asp:LinkButton>  
                    </ItemTemplate>  
                </asp:TemplateField>  
            </Columns>  
     </asp:GridView>  
      <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"  
            RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"  
            AutoGenerateColumns="false" Height="225px" Width="368px" 
            >  
         <Columns>  
                <asp:BoundField DataField="Name1" HeaderText="File Name" /> 
                <asp:BoundField DataField="Status" HeaderText="Status" />  

                 <asp:TemplateField ItemStyle-HorizontalAlign="Center">  
                    <ItemTemplate>  
                        <asp:LinkButton ID="lnkEdit" runat="server" HeaderText="Edit Status" Text="Edit Application" OnClick="EditFile1"  
                            CommandArgument='<%# Eval("Status") %>'></asp:LinkButton>  
                    </ItemTemplate>  
                </asp:TemplateField>  
            </Columns>  
     </asp:GridView>  
      <asp:GridView ID="GridView2" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"  
            RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"  
            AutoGenerateColumns="false" Height="225px" Width="368px" 
            >  
         <Columns>  
                <asp:BoundField DataField="Name1" HeaderText="File Name" /> 
                <asp:BoundField DataField="Status" HeaderText="Status" />  

                 <asp:TemplateField ItemStyle-HorizontalAlign="Center">  
                    <ItemTemplate>  
                        <asp:LinkButton ID="lnkEdit" runat="server" HeaderText="Edit Status" Text="Edit Application"   
                            CommandArgument='<%# Eval("Status") %>'></asp:LinkButton>  
                    </ItemTemplate>  
                </asp:TemplateField>  
            </Columns>  
     </asp:GridView>  

    </div>  
    </form>  
</body>  
</html>  

C#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    String statusVariable= string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
         //lb1.Text = "<b><font color=Brown>" + "WELLCOME ADMIN:: " + "</font>" + "<b><font color=red>" + Session["name"] + "</font>";
        if (!IsPostBack)
        {
            BindGrid();
        }  
    }

    private void BindGrid()
    {
        DataTable dt;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "Select Name1,Status from TBL_MST_ALL2";
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    {
                        while (reader.Read())
                        {
                            if (reader["Status"].ToString() == "3")
                            {
                                GridView.DataSource = reader;
                                GridView.DataBind();
                            }
                            else if (reader["Status"].ToString() == "2")
                            {
                                GridView1.DataSource = reader;
                                GridView1.DataBind();
                            }
                            else if (reader["Status"].ToString() == "1")
                            {
                                GridView1.DataSource = reader;
                                GridView1.DataBind();
                            }
                        }
                        con.Close();
                    }
                }
            }
        }
    }

    protected void EditFile(object sender, EventArgs e)
    {
        Response.Redirect("EditResume.aspx");
    }

    protected void EditFile1(object sender, EventArgs e)
    {
        Response.Redirect("EditResume1.aspx");
    }
}  

但是,无论状态如何,所有数据都显示在GridView1中。

0 个答案:

没有答案