how to hide column in gridview at runtime [Resolved]

Posted by Rickeybglr under ASP.NET on 9/18/2012 | Points: 10 | Views : 16823 | Status : [Member] | Replies : 5
Hi All,
i am filling my gridview by stored Procedure. this SP fetches many column, but i dnt want to show all the columns at runtime. how can i hide the column. i tried somethn lik ths gv.columns[2].visible=false; but its giving me index exception below is my code and SP.


SqlCommand cmd = new SqlCommand("USP_ExtendedSearch", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@searchUsers", txtContactsSearch.Text.Trim());
cmd.Parameters.AddWithValue("@Name", txtSearchNames.Text.Trim());
cmd.Parameters.AddWithValue("@City", txtSearchCities.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtEmail.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
conn.Close();

SP:
SELECT a.userID,a.username AS UName,a.firstname+' '+a.lastname AS Name,a.useremail AS EmailID,a.city AS City,b.imagePath
FROM userRegistrationTB a JOIN imageTB b
ON a.userID=b.UserID

i want to show only Uname,Name,EmailId,city. rest i want to hide.
plz help me out i got stuck




Responses

Posted by: Vasanthmvp on: 9/18/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

By setting autogeneratecols = false of your grid view.
and creating columns explicitly as
<asp:gridview id="grid" runat="server" >

<columns>

<asp:boundfield datafield="colname of actual table" headertext="col name you want to display" /> / Here include all the columns that you want to display. By which the columns you want to hide to the user, you can hide them.

</asp:gridview>


Regards,


Awesome Coding !! :)

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/18/2012 [Member] Starter | Points: 25

Up
0
Down
Good job Vasanth

Thanks & Regards
Hari

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sun.V on: 9/18/2012 [Member] Starter | Points: 25

Up
0
Down
Write code in Row data bound event. That means fire row data bound where ever you want write below code.
In gridview take one temlate field.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Visible="false" OnClick="btnStop_Click" />
</ItemTemplate>
</asp:TemplateField>


code


GridViewRow row = e.Row;
if (row.DataItem == null)
{
return;
}
GridView gv = new GridView();
gv = (GridView)row.FindControl("grdEnquiryChild");

gv.DataSource = ChildDataSource(((DataRowView)e.Row.DataItem)["enqId"].ToString());
gv.DataBind();
string id = ((DataRowView)e.Row.DataItem)["enqId"].ToString();
if (e.Row.RowType == DataControlRowType.DataRow)
{
Enquiry enqobj = new Enquiry();

enqobj.Status = "EnquiryProcess";
string stopedstatus = (string)enqobj.CheckInProgres(id);
if (stopedstatus != "Successful")
{
Button btnstoped = (Button)e.Row.FindControl("btnStop");
if (stopedstatus != "In Progress")
{
btnstoped.Visible = true;
btnstoped.Text = "View";
DropDownList ddlCategory = (DropDownList)e.Row.FindControl("ddlCategory");
ddlCategory.Visible = false;

}
else if (stopedstatus != "Stopped")
{
btnstoped.Visible = true;
btnstoped.Text = "Stop";
string lnk = enqobj.ExistEnqId(id);
if (lnk != "suneel")
{
HyperLink link = (HyperLink)e.Row.FindControl("hyplink");
link.ImageUrl = "~/images/attach.gif";


}
}
}

In this where ever Enquiry is progress i am showing stop button showing.
where ever it 's already i am stop i am show in view state..
if it is successful i showing visible false.



Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 9/18/2012 [Member] Starter | Points: 25

Up
0
Down
thnkss vasanth ...its working now as expected.. thnks a lot

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 10/29/2012 [Member] Starter | Points: 25

Up
0
Down
hi..

try this code....it is not required to use the visible property at design time....

It can hide the column at run time....according to your requirement.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

{
e.Row.Cells[column_index].Visible = false;

// column_index can be 0,1,2,3.....N
}


Sourabh07

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response