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....

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.