Cấp bậc tác giả:

DOTNET

Ẩn hoặc hiển thị cột trên GridView bởi Column Index

Được viết bởi webmaster ngày 21/02/2015 lúc 11:15 PM
Bài viết này sẽ chỉ cho bạn cách ẩn hoặc hiển thị các cột trên GridView bằng chỉ mục cột. Ví dụ này, tôi sẽ hiển thị ràng buộc tổng giá trị.
  • 0
  • 15189

Ẩn hoặc hiển thị cột trên GridView bởi Column Index

Đầu tiên tôi tạo ứng dụng asp.net mới và thêm đoạn code dưới đây vào trang.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Hide Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False"
                    ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Bold="True" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Address">
                            <ItemTemplate>
                                <asp:Label ID="lblNamAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Type">
                            <ItemTemplate>
                                <asp:Label ID="lblUserType" runat="server" Text='<%# Eval("UserType") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterStyle HorizontalAlign="Right" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Earning">
                            <ItemTemplate>
                                <asp:Label ID="lbltotal" runat="server" Text='<%# Eval("Earning") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Trong đoạn mã trên, tôi đã sử dụng ItemTemplate để ràng buộc các trường trên GridView và sử dụng control label để hiển thị giá trị sử dụng Eval. Trong này tôi đã cung cấp giá trị cho mỗi tiêu đề. Bây giờ kiểm tra mã để ràng buộc và không hiển thị.

using System;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataTable objdt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDistinctValue();
            }
        }
        private void GetDistinctValue()
        {
            SqlConnection con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {

                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
               /*Hide*/
            e.Row.Cells[3].Visible = false;

            /*Show*/
            //e.Row.Cells[3].Visible = true;
        }
    }
}

Mã trên tôi đã sử dụng chức năng ràng buộc dữ liệu. Bây giờ kiểm tra mã dưới đây để ẩn cột.

1-gridview.png

Dưới đây là kết quả:

2-hide-column.png

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML