hi...
i want to hide a column in gridview..but must be able to access the data from that column..

whenever i make the column visibility to false..i wont be able to access the data..
it works..only when the condition is true.
I tried the below method also..
GridView1.Columns.Item(0).ItemStyle.Width = 0
still the problem is not solved

the problem is solved only when there ocurs 1 entry in the gridview..

Given Below is my code..
here the variable p doesnot get any value when there is more than 1 entry in gridview


For i = 0 To GridView1.Rows.Count - 1
chk = GridView1.Rows(i).FindControl("CheckBox1")
If chk.Checked = True Then
p = GridView1.Rows(i).Cells(3).Text
End If
Next


Please help me to solve this problem....

Dani AI

Generated

the issue is that setting a BoundField or TemplateField to Visible = False prevents that cell from being created for each row. Once the column is removed, the cell indexes shift and Cells(3) is no longer what you expect, which is why it only seems to work in some cases. Do not rely on Cells(n) when you hide columns this way.

Use DataKeys to carry the hidden value you need, and read it from the selected row. This keeps the value out of the UI but still available in code, and you do not have to add extra controls. In your GridView markup, set DataKeyNames to the field you want to stash (you can include multiple keys, comma-separated). Then:

' assume GridView1.DataKeyNames = "HiddenValue"
Protected Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
    For Each row As GridViewRow In GridView1.Rows
        Dim chk As CheckBox = TryCast(row.FindControl("CheckBox1"), CheckBox)
        If chk IsNot Nothing AndAlso chk.Checked Then
            Dim p As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Values("HiddenValue"))
            ' use p here
        End If
    Next
End Sub

If you still want the column rendered but not visible, avoid Columns(i).Visible = False. Instead hide per-row so the cells still exist:

Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.Header OrElse e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(0).Visible = False
        e.Row.Cells(3).Visible = False
    End If
End Sub

That approach plays nicely with your loop and with ’s suggestion. If you use paging or sorting, DataKeys remains the most robust choice.

Recommended Answers

All 3 Replies

Hi, Why don't you use hiddenfield instead of populating in column and find in following way

p=GridView1.Rows(i).FindControl("hfMyText").value

Actually..my gridview consist of 4 columns..
i want to hide 0th and 3rd column during runtime..
1st column is a name..and 2nd is a chekbox..when i select the checkbox,and on a button click..the data in 3rd column(the one which is hidden)must be saved in the variable "p"

I gave:

Page load:
GridView1.Columns.Item(0).ItemStyle.Width = 0
GridView1.Columns.Item(3).ItemStyle.Width = 0
GridView1.Columns(0).Visible = False
GridView1.Columns(3).Visible = False

button click:

Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Try
            Dim i As Integer
            Dim chk As New CheckBox
            For i = 0 To GridView1.Rows.Count - 1
                chk = GridView1.Rows(i).FindControl("CheckBox1")
                If chk.Checked = True Then
                  p = GridView1.Rows(i).Cells(3).Text
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

Problem is occuring when

GridView1.Columns(0).Visible = False
GridView1.Columns(3).Visible = False

when the visibility is true..the variable "p" gets the value from 3rd column

Hi,

I think you should try adding your data inside literal control within template field in the following format.

<asp:TemplateField>
	<HeaderTemplate>Data Text</HeaderTemplate>
		<ItemTemplate>
			<asp:literal ID="ltrDataText" Text='<%#DataBinder.Eval(Container,"DataItem.DataText") %>' runat="server" />
		</ItemTemplate>
</asp:TemplateField>

Literal don't add extral rendering to your source and won't be bulky.
further, you can get your value in a way shown below.

Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
            For Each row As GridViewRow In gvPopulate.Rows
                Dim chkSelect As CheckBox = CType(row.FindControl("chkSelectName"), CheckBox)
                If chkSelect.Checked Then
                    Dim p As String = CType(row.FindControl("ltrDataText"), Literal).Text
                    Response.Write(p & ", ")
                End If
            Next
        End Sub

I recently created a mock up for this and is working fine. Would you mail me your email add. so i can send it to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.