protected void Page_Load(object sender, EventArgs e)
{
string Constring = "Data Source=.\\SQLEXPRESS;Initial Catalog=Learn;Integrated Security=True";
using (SqlConnection cn = new SqlConnection(Constring))
{
cn.Open();
string Query = "select productid,productname from product";
SqlCommand cmd = new SqlCommand(Query, cn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataKeyNames = new string[] { "productid" };
GridView1.DataSource = ds;
GridView1.DataBind();
cn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = GridView1.Rows[1].Cells[0].Text;
}
I want to hide the first or other columns even if they are not primary keys. In the above HTML code, I set the TemplateField's visible property to false. This works, but on button click I want to fetch the cell value. After hiding, I am not able to access
the hidden column values.
...... but i think you wan to access the Product Id of the selected column. Fo this you no need to have hidden columns, U just add ProductId into your datakeynames in ut HTML code and then in the click event you can get the ProductId of the selected row
as follows:
Dim ProductId as Integer=Gridview1.Datakeys[Gridview1.row[0].index]["ProductId"];
my HTML source:
<asp:GridView ID="gvEmployee" runat="server" AllowPaging="True"
AutoGenerateColumns="False" AllowSorting="True" Width="100%"
EmptyDataText="No records to display!"
ShowHeaderWhenEmpty="True"
DataKeyNames="EmployeeId" >
My VB.Net:
Protected Sub btnUnassign_Click(sender As Object, e As EventArgs) Handles btnUnassign.Click
Try
If Not Session("ID") Is Nothing Then
For Each gvr As GridViewRow In gvEmployee.Rows
Dim chk As CheckBox = New CheckBox
chk = DirectCast(gvr.FindControl("chkSelect"), CheckBox)
If chk.Checked = True Then
EmployeeId = gvEmployee.DataKeys(gvr.RowIndex)("EmployeeId")
''Save to DB
End If
Next
End If
Catch ex As Exception
lblErrorMessage.Text = ex.Message.ToString
End Try
End Sub
... when you want to access it, just change the visible property as true and you can access the values...
Using the Datakeys.... u can have more columns in Gridviews datakeys and you can access with their names...
DataKeyNames="EmployeeId,Name"
VB.Net Code:
Protected Sub gvEmployee_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEmployee.RowDataBound
Try
''For Hyperlink
Dim hlk As HyperLink = New HyperLink
hlk = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink)
If Not hlk Is Nothing Then
hlk.Text = Convert.ToString(gvEmployee.DataKeys(e.Row.RowIndex)("Name"))
hlk.NavigateUrl = "EmployeeMaster.aspx?Edit=Y&&ID=" & Convert.ToString(gvEmployee.DataKeys(e.Row.RowIndex)("EmployeeId"))
End If
Catch ex As Exception
lblErrorMessage.Text = ex.Message.ToString
End Try
End Sub
Hey, where is the button?
If you either add a button to your template or make your label a link button you can pass in the id on the CommandArgument and do your processing server side
Something like this inside your template
<asp:LinkButton ID="lBtnProduct" CommandArgument='<%#Eval("productid") %>'
runat="server" CssClass="CommandButton" OnClick="lBtnProduct_Click" Text='<%#Eval("productname") %>'></asp:LinkButton>
Also unless you are doing two way binding, you should use the Eval for better performance.
Something like this on the server:
BTW imageButton and LinkButton have the same event signature so you could use this method for both
Protected Sub lBtnProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbtn As New LinkButton
Dim ibtn As New ImageButton
Dim cmd As String = ""
Try
If sender.ToString = "System.Web.UI.WebControls.ImageButton" Then
ibtn = DirectCast(sender, ImageButton)
cmd = ibtn.CommandArgument
Else
lbtn = DirectCast(sender, LinkButton)
cmd = lbtn.CommandArgument
End If
'Do some processing on the server using the selected ProductID
Catch ex As Exception
LogException(ex)
lblStatus.Text = "Error: " & ex.Message
Finally
If Not lbtn Is Nothing Then
lbtn.Dispose()
End If
If Not ibtn Is Nothing Then
ibtn.Dispose()
End If
End Try
End Sub
rpk2006
Member
631 Points
629 Posts
How to hide a GridView column but still keep it accessible?
Dec 02, 2012 09:55 AM|LINK
I looked into various samples to hide a column. My GridView HTML code looks like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productid"> <Columns> <asp:TemplateField Visible="false"> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text ='<%#Bind("productid") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="productname" /> </Columns> </asp:GridView>The code-behind is:
protected void Page_Load(object sender, EventArgs e) { string Constring = "Data Source=.\\SQLEXPRESS;Initial Catalog=Learn;Integrated Security=True"; using (SqlConnection cn = new SqlConnection(Constring)) { cn.Open(); string Query = "select productid,productname from product"; SqlCommand cmd = new SqlCommand(Query, cn); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataKeyNames = new string[] { "productid" }; GridView1.DataSource = ds; GridView1.DataBind(); cn.Close(); } } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = GridView1.Rows[1].Cells[0].Text; }I want to hide the first or other columns even if they are not primary keys. In the above HTML code, I set the TemplateField's visible property to false. This works, but on button click I want to fetch the cell value. After hiding, I am not able to access the hidden column values.
jack_shivani...
Member
276 Points
72 Posts
Re: How to hide a GridView column but still keep it accessible?
Dec 02, 2012 10:20 AM|LINK
Hi,
you can hide the columns after your databind...
Gridview1.column[0].visible=false;
...... but i think you wan to access the Product Id of the selected column. Fo this you no need to have hidden columns, U just add ProductId into your datakeynames in ut HTML code and then in the click event you can get the ProductId of the selected row as follows:
Dim ProductId as Integer=Gridview1.Datakeys[Gridview1.row[0].index]["ProductId"];
my HTML source: <asp:GridView ID="gvEmployee" runat="server" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" Width="100%" EmptyDataText="No records to display!" ShowHeaderWhenEmpty="True" DataKeyNames="EmployeeId" > My VB.Net: Protected Sub btnUnassign_Click(sender As Object, e As EventArgs) Handles btnUnassign.Click Try If Not Session("ID") Is Nothing Then For Each gvr As GridViewRow In gvEmployee.Rows Dim chk As CheckBox = New CheckBox chk = DirectCast(gvr.FindControl("chkSelect"), CheckBox) If chk.Checked = True Then EmployeeId = gvEmployee.DataKeys(gvr.RowIndex)("EmployeeId") ''Save to DB End If Next End If Catch ex As Exception lblErrorMessage.Text = ex.Message.ToString End Try End Subrpk2006
Member
631 Points
629 Posts
Re: How to hide a GridView column but still keep it accessible?
Dec 02, 2012 10:24 AM|LINK
What if I have two or more hidden columns and I want to get the value for cell 3?
jack_shivani...
Member
276 Points
72 Posts
Re: How to hide a GridView column but still keep it accessible?
Dec 02, 2012 10:38 AM|LINK
you can hide any no. of columns...
Gridview1.columns[0].visible=false;
Gridview1.columns[2].visible=false;
Gridview1.columns[5].visible=false;
... when you want to access it, just change the visible property as true and you can access the values...
Using the Datakeys.... u can have more columns in Gridviews datakeys and you can access with their names...
DataKeyNames="EmployeeId,Name" VB.Net Code: Protected Sub gvEmployee_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEmployee.RowDataBound Try ''For Hyperlink Dim hlk As HyperLink = New HyperLink hlk = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink) If Not hlk Is Nothing Then hlk.Text = Convert.ToString(gvEmployee.DataKeys(e.Row.RowIndex)("Name")) hlk.NavigateUrl = "EmployeeMaster.aspx?Edit=Y&&ID=" & Convert.ToString(gvEmployee.DataKeys(e.Row.RowIndex)("EmployeeId")) End If Catch ex As Exception lblErrorMessage.Text = ex.Message.ToString End Try End SubFrank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to hide a GridView column but still keep it accessible?
Dec 04, 2012 07:42 AM|LINK
If you want to access hidden columns of GridView, please refer to this two approaches in the following article:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/22/faq-how-to-get-hidden-columns-value-in-gridview.aspx
Furthermore, you can hide Gridview column programmatically in RowDataBound or OnRowCreated event:
http://www.devcurry.com/2009/05/hiding-column-in-aspnet-gridview.html
http://ramanisandeep.net/2009/04/07/how-to-hide-gridview-column-programmatically/
Feedback to us
Develop and promote your apps in Windows Store
shahid.majee...
Member
620 Points
543 Posts
Re: How to hide a GridView column but still keep it accessible?
Dec 04, 2012 07:58 PM|LINK
try this
protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ((Label)GridView1.Rows[1].FindControl("lblId")).Text; } // check your lable control idShahid Majeed
Email: shahid.majeed@gmail.com
Sean Gahan
Member
85 Points
14 Posts
Re: How to hide a GridView column but still keep it accessible?
Dec 04, 2012 09:47 PM|LINK
Hey, where is the button?
If you either add a button to your template or make your label a link button you can pass in the id on the CommandArgument and do your processing server side
Something like this inside your template
<asp:LinkButton ID="lBtnProduct" CommandArgument='<%#Eval("productid") %>'
runat="server" CssClass="CommandButton" OnClick="lBtnProduct_Click" Text='<%#Eval("productname") %>'></asp:LinkButton>
Also unless you are doing two way binding, you should use the Eval for better performance.
Something like this on the server:
BTW imageButton and LinkButton have the same event signature so you could use this method for both
Protected Sub lBtnProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbtn As New LinkButton
Dim ibtn As New ImageButton
Dim cmd As String = ""
Try
If sender.ToString = "System.Web.UI.WebControls.ImageButton" Then
ibtn = DirectCast(sender, ImageButton)
cmd = ibtn.CommandArgument
Else
lbtn = DirectCast(sender, LinkButton)
cmd = lbtn.CommandArgument
End If
'Do some processing on the server using the selected ProductID
Catch ex As Exception
LogException(ex)
lblStatus.Text = "Error: " & ex.Message
Finally
If Not lbtn Is Nothing Then
lbtn.Dispose()
End If
If Not ibtn Is Nothing Then
ibtn.Dispose()
End If
End Try
End Sub
Sean Gahan
http://SeanGahan.Net