Codeaddict7423
asked on
asp.net c# gridview columns hide
Hello,
I have a *.aspx page where I allow users to search a database table via stored procedure.
When the user clicks an asp.button called "find", it fires the stored procedure which populates a gridview control.
I am attempting to hide two columns on this gridview and cannot seem to accomplish that.
My *.aspx code follows:
---------------
<asp:SqlDataSource ID="ds_warrantsearchresult s" runat="server" ConnectionString="<%$ ConnectionStrings:Jailinfo _JAUConnec tionString 1 %>"
SelectCommand="sp_getAllWa rrantsbyNa me" SelectCommandType="StoredP rocedure">
<SelectParameters>
<asp:ControlParameter ControlID="FirstNameTextBo x" Name="Firstname" PropertyName="Text"
Type="String" Size="50" ConvertEmptyStringToNull=" false" />
<asp:ControlParameter ControlID="LastNameTextBox " Name="Lastname" PropertyName="Text"
Type="String" ConvertEmptyStringToNull=" false" />
<asp:ControlParameter ControlID="SpnTextBox" Name="Spn" PropertyName="Text" Type="String"
ConvertEmptyStringToNull=" false" />
<asp:ControlParameter ControlID="DobTextBox" Name="Dob" PropertyName="Text" Type="String"
ConvertEmptyStringToNull=" false" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv_warrantsearchresult s" runat="server" DataKeyNames="Spn" AllowSorting="True"
DataSourceID="ds_warrantse archresult s" CellPadding="4" Font-Names="Arial" Font-Size="Small"
ForeColor="#0033CC" GridLines="None" Width="580px" EmptyDataText="NO Matching Data Found"
OnSelectedIndexChanged="gv _warrantse archresult s_Selected IndexChang ed" PageSize="15"
ShowFooter="True" AllowPaging="true">
<%-- <HeaderStyle CssClass="GridHeader" /> --%>
<RowStyle BackColor="#EFF3FB" />
<EmptyDataRowStyle Font-Names="Arial" Font-Size="Small" ForeColor="Red" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" Font-Names="Arial"
Font-Size="X-Small" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle BackColor="#507CD1" ForeColor="#507CD1" HorizontalAlign="center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" VerticalAlign="Top" />
</asp:GridView>
-----------
The codebehind of the asp.button that fires the stored procedure follows:
-----------
protected void Button1_Click(object sender, EventArgs e)
{
pnl_gvWarrantSearchResults .Visible = true;
//gv_warrantsearchresults. Visible = true;
gv_warrantsearchresults.Co lumns[0].V isible = true;
gv_warrantsearchresults.Co lumns[1].V isible = true;
//gv_warrantsearchresults. Columns[2] .Visible = true;
//gv_warrantsearchresults. Columns[3] .Visible = true;
//gv_warrantsearchresults. Columns[4] .Visible = false;
pnl_gvSearchResults_Detail sview.Visi ble = true;
dv_searchresults.Visible = true;
pnl_wsdetail_imageview.Vis ible = true;
//pnl_agreetoterms.Visible = true;
//txt_AgreedToTerms.Text = "You Hereby Agree to Terms & Conditions";
pnl_display_ddl.Visible = true;
}
---------------
ANY HELP with this would be greatly appreciated.
I have a *.aspx page where I allow users to search a database table via stored procedure.
When the user clicks an asp.button called "find", it fires the stored procedure which populates a gridview control.
I am attempting to hide two columns on this gridview and cannot seem to accomplish that.
My *.aspx code follows:
---------------
<asp:SqlDataSource ID="ds_warrantsearchresult
SelectCommand="sp_getAllWa
<SelectParameters>
<asp:ControlParameter ControlID="FirstNameTextBo
Type="String" Size="50" ConvertEmptyStringToNull="
<asp:ControlParameter ControlID="LastNameTextBox
Type="String" ConvertEmptyStringToNull="
<asp:ControlParameter ControlID="SpnTextBox" Name="Spn" PropertyName="Text" Type="String"
ConvertEmptyStringToNull="
<asp:ControlParameter ControlID="DobTextBox" Name="Dob" PropertyName="Text" Type="String"
ConvertEmptyStringToNull="
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv_warrantsearchresult
DataSourceID="ds_warrantse
ForeColor="#0033CC" GridLines="None" Width="580px" EmptyDataText="NO Matching Data Found"
OnSelectedIndexChanged="gv
ShowFooter="True" AllowPaging="true">
<%-- <HeaderStyle CssClass="GridHeader" /> --%>
<RowStyle BackColor="#EFF3FB" />
<EmptyDataRowStyle Font-Names="Arial" Font-Size="Small" ForeColor="Red" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" Font-Names="Arial"
Font-Size="X-Small" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle BackColor="#507CD1" ForeColor="#507CD1" HorizontalAlign="center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" VerticalAlign="Top" />
</asp:GridView>
-----------
The codebehind of the asp.button that fires the stored procedure follows:
-----------
protected void Button1_Click(object sender, EventArgs e)
{
pnl_gvWarrantSearchResults
//gv_warrantsearchresults.
gv_warrantsearchresults.Co
gv_warrantsearchresults.Co
//gv_warrantsearchresults.
//gv_warrantsearchresults.
//gv_warrantsearchresults.
pnl_gvSearchResults_Detail
dv_searchresults.Visible = true;
pnl_wsdetail_imageview.Vis
//pnl_agreetoterms.Visible
//txt_AgreedToTerms.Text = "You Hereby Agree to Terms & Conditions";
pnl_display_ddl.Visible = true;
}
---------------
ANY HELP with this would be greatly appreciated.
I used this once and it worked :
protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( condition)
{
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
..
protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( condition)
{
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
..
Use the gridview's rowdatabound event and change the "visible" property to false:
e.row.cells(0).visible = false;
Or change the display attribute to "none":
e.row.cells(0).style.add(" display", "none");
e.row.cells(0).visible = false;
Or change the display attribute to "none":
e.row.cells(0).style.add("
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
see this as well:-http://www.codeproject.com/Articles/43727/Show-Hide-GridView-Columns-in-ASP-NET