Hide row in Gridview asp.net - c#

I am trying to hide a row when its button clicked in my gridview. Does anyone have a solution for this, I am all yours,
Code behind:
protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "removethis")
{
Guid id = Guid.Parse(e.CommandArgument.ToString());
UsersBL.DeleteUserByUserId(id);
}
else
{
//hide the row
}
}

try this - Add RowDataBound event and do like this:
e.Row.Visible = false;

You need to access the row-object and then you can hide it, it's a little tricky:
protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "removethis")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
row.Visible = false;
}
}

Related

Hiding rows from gridview based on a value from field in that row

I have a grid view and in this grid some rows have a field called "Department". What I want is a code that reads that fields value and then if it equals a string "Industrial" the row should be hidden and not shown.
I tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (e.Row.Cells[4].Text == "industrial")
e.Row.Visible = false;
}
But it keeps saying (e) is not defined, and there is no such thing as (.Row).
It's GridViewRowEventArgs that have access to e.Row but it's used in RowCreated and RowDataBound events. You can use SelectedRow property of GridView instead.
var gridView = (GridView)sender;
if (gridView.SelectedRow.Cells[4].Text == "industrial")
gridView.SelectedRow.Visible = false;
Updated:
To hide the rows when page is loaded, place the for loop inside the Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
// ...
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[4].Text == "industrial")
GridView1.Rows[i].Visible = false;
}
}

unable to retain the values for dynamic creation of grid rows after print

i have a gridview in which i have created dynamic rows in row created event as shown in below code.
protected void grdPBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
strPreviousRowID = DataBinder.Eval(e.Row.Date).ToString()}
grdPBook.ShowFooter = false;
}
}
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
bool IsSubTotalRowNeedToAdd = false;
bool IsGrandTotalRowNeedtoAdd = false;
if (ddlSummary.SelectedValue == "0")
{
if ((strPreviousRowID != string.Empty)(DataBinder.Eval(e.Row.DataItem,Date) != null))
{
if (strPreviousRowID != DataBinder.Eval(e.Row.DataItem, Date).ToString())
{
if (ddlSummary.SelectedValue == "0")
{
IsSubTotalRowNeedToAdd = true;
}
}
}
if (IsSubTotalRowNeedToAdd)
{
// ---code for adding dynamic subtotal row-----
}
}
}
when i print the grid the print dialog opens and after closing the dialog the dynamically generated columns of grid disappers and the grid gets messed up coz i m not able to retain the values for Date(here)on the basis of which the dynamic rowsare generated.
How can i achieve the task.Help me.
protected void grdPBook_RowCreated(object sender, GridViewRowEventArgs e)
{
// your code
if (ddlSummary.SelectedValue == "0")
{
//your code
grdPBook.DataSource = dt;
//here dt is your Data Table object containing all rows.
}
grdPBook.DataBind();
}
Use grdPBook.DataBind(); in every Row operations.

Why can I not catch the value of Gridview row?

Here is my code.
problem: When click om a row och on select the page is refreshing and i dont get the text in the lable17.text.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Label17.Text = row.Cells[2].Text.ToString() ;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Label17.Text = "you selected" + row.Cells[2].Text;
}
Is your GridView in an UpdatePanel? If not the entire Page will postback when you click on the Button. Also, make sure that if you are setting the Text of Label17 in the Page_Load event that you only do it the first time i.e.
public void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Label17.Text = "Default Text";
}
}

Sorting Gridview breaks ModalPopUp in GridView

I have a gridview that has linkbuttons that call modalpopups and textboxes with values. I am trying to implement sorting for the gridview, but the if(!ispostback) statement I need for sorting prevents the modalpopup from appearing. It also does not sort the textboxes in the gridview. Is there a way to implement sorting without using ispostback in the page_load?
Here is the code for the modalpopup, gridview binding and sorting.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sortOrder"] = "";
Bind_Gridview("", "");
loadModals();
}
}
protected void viewModal(object sender, EventArgs e)
{
...
mainPanel.Controls.Add(exstModal);
mainPanel.Controls.Add(exstModalBox);
exstModalBox.Show();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
Bind_Gridview(e.SortExpression, sortOrder);
}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
protected void gv1_RowCommand(object sender, GridViewRowEventArgs e)
{
...
CheckBox cb = new CheckBox();
TextBox ca = new TextBox();
ca.Width = 20;
TextBox cga = new TextBox();
cga.Width = 20;
if (e.Row.RowType == DataControlRowType.DataRow) //Foreach row in gridview
{
while (dr1.Read())
{
ca.Text = dr1["cyla"].ToString();
cga.Text = dr1["cga"].ToString();
checkText = dr1["completed"].ToString();
if (checkText == "True")
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
...
dr1.Close();
conn1.Close();
e.Row.Cells[6].Controls.Add(ca);
e.Row.Cells[8].Controls.Add(cga);
e.Row.Cells[9].Controls.Add(cb);
...
}
A GridView has built-in sorting capabilities. Depending on the dataset you are using to populate the data, you likely don't need to manually handle anything manually, let alone with the ViewState.
Check out the second example on this MSDN page and note that it never does anything manually with the ViewState... the OnSorting and OnSorted events are there just to display extra information or to impose requirements:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx
If you post a bit more of your code (including your .aspx pages, the markup for the modal popups, and the code for the loadModals() function, we might be able to better help you.

dropdownlist in footer row of gridview being clear on postback

I am using a dropdownlist in footer row of gridview(ASP.Net) and I fill that on rowdatabound event,first time it works fine but when the form is postbacked,dropdown gets cleared.
It can be solved by refilling it on each postback,but I want that only single time code binding call fulfill my need,
means is there any way to stop from being null on postback.
looking for your kind solutions and suggestions
Thnx in advance......
Supriya
Code:
protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (gvProjects.Rows.Count > 0 && e.Row.RowIndex != -1)
{
string proj_Id = Convert.ToString(gvProjects.DataKeys[e.Row.RowIndex].Value);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlProject = (DropDownList)e.Row.FindControl("ddlProject");
if (ddlProject != null && ddlProject.Items.Count == 0)
{
objMaster.FillProjects(ddlProject);
ddlProject.SelectedValue = proj_Id;
}
}
}
}
catch (Exception excep)
{
lbl_msg.Text = excep.Message;
}
}
It's called whenever the grid is binded,can it be avoided.
With this code you will avoid filling the dropdownlist located in the gridview footer in each rowdatabound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Footer)
{
//do the binding for the normal rows
}
}
As you can see, the rowdatabound will be only executed on the header and normal rows but not in the footer.
Hope this helps!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropdown();
}
}

Categories

Resources