본문 바로가기
마루아라는 개발쟁이/ASP.NET

GridView의 field를 hidden field로

by 마루아라 이야기 2022. 12. 6.
반응형

반나절의 허접질에서 드디어 벗어났다...

 

GridView의 특정 field를 hidden field로 맹글기..

 

<asp:GridView ID="SetItemList" runat="server" AutoGenerateColumns="False" OnRowCreated="SetItemList_OnRowCreated">
  <Columns>
       <asp:BoundField DataField="nDevID" />
       <asp:BoundField DataField="nInstance" />
       <asp:BoundField DataField="sItemID" />
  </Columns>

</asp:GridView>

 

 

위 nDevID, nInstance, sItemID 의 필드 중 sItemID를 보여주지 않으려 한다.


visible = false;

 

위 방법을 이용하게 되면 보여지지 않게 하는게 아니라 그 필드 자체가 없어진다..

 

하지만...

 

OnRowCreated 를 이용해서

 

protected void SetItemList_OnRowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[3].Visible = false;
}

위와 같이 처리해 주면 신기하게도 소스 보기를 해도 보이지 않지만 CS에서 찍어 보면 그 값을 받아올 수 있다...

 

참고 자료 :

 

GridView is one of the new controls in ASP.Net 2.0 that replaced the DataGrid. If you've been working in ASP.Net 2.0 since some time, you would have come to face a problem. When you design the GridView and create a bound field... In earlier versions of .Net when you didn't want the data to be visible to the client, but wanted the column for processing of data, you simply set the Column visibility to False.

However, this does not work in ASP.Net 2.0. When a column's visibility is set to False, then the Grid does not bind data to the column, and thus when you try to retrieve data from the hidden column, it either blows up or returns an empty string.

This presents a really big problems to developers, and in this small article I present a solution to this dilemma. I hope this can be helpful for your development needs.

Prerequisites

This tutorial assumes that you own a copy of Visual Studio 2005 or Visual Web Developer Express. It also assumes that you are familiar with ASP.Net 2.0 basics.

The Solution

Create the Row Created event handler and do the following:

Public Sub myGrid_OnRowCreated(ByVal sender As Object, ByVal e As Web.UI.WebControls.GridViewRowEventArgs) Handles myGrid.RowCreated

        'Those columns you don't want to display you config here,

        'you could use a for statement if you have many :)

        e.Row.Cells(1).Visible = False

End Sub

 

Why this works? Because the event is called after the data is bound to the grid... This ensures that the column has been databound and then it is hidden.

Another solution can be to use the following to bind the data to the GridView.

Public Sub myTestFunction()

        'To hide a column, set its width to zero or use MappingType

        Dim strCON As String = "<Connection String>"

        Dim strQuery As String = "<QueryString>"

        Dim da As Data.SqlClient.SqlDataAdapter

        Dim ds As Data.DataSet

        Try

            Dim conn As New Data.SqlClient.SqlConnection(strCON)

            da = New Data.SqlClient.SqlDataAdapter(strQuery, CON)

            ds = New Data.DataSet

            da.Fill(ds, "tblData")

            conn.Close()

        Catch ex As Exception

            'Do error handling here...

        End Try

        'Here you can HIDE the Column

        ds.Tables("tblData").Columns(colIndex).ColumnMapping = Data.MappingType.Hidden

        myGrid.DataSource = ds.Tables("tblData")

End Sub

728x90
반응형
LIST