I have a GridView that displays some information about a job and one of the options in the GridView is to select a reason. If a reason isn't selected then I don't want to display that row in the GridView. How do I check if there is a value in the Reason column, and if its empty how do I hide that row?
private GridView BuildDebriefGridView()
{
GridView gv = new GridView();
gv.AutoGenerateColumns = false;
//gv.RowDataBound +=
gv.Columns.Add(new BoundField { HeaderText = "Job No", DataField = "JobNo" });
gv.Columns.Add(new BoundField { HeaderText = "Qty Rcvd", DataField = "QtyRcvd" });
gv.Columns.Add(new BoundField { HeaderText = "Reason", DataField = "Reason" });
gv.Columns.Add(new BoundField { HeaderText = "Comment", DataField = "Comment" });
gv.Attributes.Add("style", "width:100%");
return gv;
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
JobPieceSerialNo item = e.Row.DataItem as JobPieceSerialNo;
if (item != null)
{
if (string.IsNullOrEmpty(item.Reason))
{
e.Row.Visible = false;
}
}
}
}
Try following;
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells["Reason"].Text == "")
e.Row.Visible = false;
}
Or if it is a checkbox
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(((CheckBox)e.Row.FindControl("YourCheckboxID")).Checked == false)
e.Row.Visible = false;
}
Using GridView_RowDataBound event you can achieve this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[ReasonCellNumber].Text == "Reason")
{
e.Row.Visible = false;
}
}
}
Related
I am using the below code to disable row in gridview where the column name is test. Everything works fine except for the first row. The color does not get applied to the first row. Where am I going wrong?I also want to hide a column based on column name.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Hidden4.Value == "Data Present")
{
foreach (GridViewRow item in GdvTestData.Rows)
{
int a = GetColumnIndexByName(item, "test");
int b = GetColumnIndexByName(item, "id");
if (e.Row.RowType == DataControlRowType.DataRow)
{
string test = e.Row.Cells[a].Text;
foreach (TableCell cell in e.Row.Cells)
{
if (test == "Y")
{
cell.BackColor = Color.Gray;
e.Row.Attributes.Add("onmouseover", "alert('This data is for testing');");
}
}
e.Row.Cells[b].Visible = false;
}
}
}
}
Maybe you are confusing data/business logic and UI.
RowDataBound event works per row so you don't need to loop gridview rows.
You are working on cell content but you have data bound to those. So:
ASPX
<asp:GridView runat="server" ID="gv" ...
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("Test") %>' />
</ItemTemplate>
</asp:TemplateField>
...
Code Behind
private void GrdBind()
{
// populate datatable
gv.DataSource = myDataTable;
gv.Databind();
}
And the databound event:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Hidden4.Value == "Data Present")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string test = row.Field<String>("Test");
if (test== "Y")
{
e.Row.Cells[0].BackColor = Color.Gray; // set your index
e.Row.Attributes.Add("onmouseover", "alert('This data is for testing');");
}
}
}
}
You can do this. Although I don't understand what you mean by disabling a row.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//find the column value in the row, not the cell text
string test = row["columnName"].ToString();
//check the value and if so apply backcolor
if (test == "Y")
{
e.Row.BackColor = Color.Red;
//or hide the row (no need to do this in Databind or OnPreRender)
e.Row.Visible = false;
}
}
}
I have a gridview and I am binding the data through Datareader. In database t able this is a bit field and I want to check this that if it's FALSE hide some controls in gridview. so how can I get this value while Rowdatabound event. Thank you
here is my code.
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.Cells[2]);
if (c.Checked)
{
LinkButton1.Visible = true;
LinkButton2.Visible = true;
}
else
{
LinkButton1.Visible = false;
LinkButton2.Visible = false;
}
}
}
You can directly use database field as mentioned below:
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
// CheckBox c = ((CheckBox)e.Row.Cells[2]);
LinkButton1.Visible = isChecked;
LinkButton2.Visible = isChecked;
}
}
See below two lines of code in above code:
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
Try this
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.FindControl("idOfCheckBox");
LinkButton1.Visible = LinkButton2.Visible = c.Checked
}
}
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.
here i bind grid columns dynamically using code behind because my GetSocailAnalytics method return dynamic column according to parameter passed.after binding column of grid using data table my grd_OnRowCommand event not firing when i click on grid row. grid is successfully bind. can any one help me out this issue.here is my code...
<asp:GridView ID="grd" EnableViewState="true" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand"
runat="server" OnRowDataBound="grd_RowDataBound">
<Columns>
</Columns>
</asp:GridView>
private void GetData()
{
try
{
int TotalRecords = 0;
DataTable dt = ClsSocialManager.GetSocialAnalytics(Convert.ToInt32(hdnReferrerId.Value), Convert.ToInt32(hdnReferralId.Value), out TotalRecords, Convert.ToInt32(hdnPageIndex.Value));
if (dt != null && dt.Rows.Count > 0)
{
BindTemplateFiled(dt);
grd.Visible = true;
}
else
{
grd.Visible = false;
}
lblStatus.Text = TotalRecords.ToString() + " Record(s) found";
}
catch (Exception ex)
{
lblStatus.Text = "Some Error Occured " + ex.Message;
lblStatus.CssClass = "ErrMsg";
}
}
//Start Crearting GridColumn Dynamically
class LinkColumn : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
LinkButton link = new LinkButton();
link.ID = "lnkbtnReferrerHost";
link.DataBinding += new EventHandler(this.link_DataBinding);
link.CommandName = "sad";
container.Controls.Add(link);
}
private void link_DataBinding(Object sender, EventArgs e)
{
LinkButton lnkReferrerHost = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkReferrerHost.NamingContainer;
lnkReferrerHost.Text = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[1]);
lnkReferrerHost.CommandArgument = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[0]);
//lnkReferrerHost.CommandName = "Filter";
}
}
private void BindTemplateFiled(DataTable dt)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].ColumnName == "Referrer Host")
{
var lnkbtnReferrerHost = new TemplateField();
lnkbtnReferrerHost.ItemTemplate = new LinkColumn();
lnkbtnReferrerHost.HeaderText = dt.Columns[i].ColumnName;
grd.Columns.Add(lnkbtnReferrerHost);
}
else
{
BoundField field = new BoundField();
field.DataField = dt.Columns[i].ColumnName;
field.HeaderText = dt.Columns[i].ColumnName;
grd.Columns.Add(field);
}
}
grd.DataSource = dt;
grd.DataBind();
}
//End Crearting GridColumn Dynamically
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Filter")
{
GridViewRow gvr = (GridViewRow)grd.Rows[((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex];
LinkButton lbtn = (LinkButton)gvr.FindControl("lnkReferrerHost");
hdnReferrerId.Value = Convert.ToString(Convert.ToInt32(e.CommandArgument));
lblCurrentPage.Text = lbtn.Text;
GetData();
}
}
catch
{
}
}
The RowCommand event is raised when a button is clicked in the
GridView control.
it is not not firing when you click on grid row
solution is adding button (LinkButton) column to the gridview with CommandName ="Filter"
i have this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Style.Add("text-align", "center");
}
i want to make the header of this cell invisible.. how can i do this by just knowing the cell value...
it does not find the header... i am creating the header at runtime... like this
GridView Grid = new GridView();
Grid.RowDataBound += Grid_RowDataBound;
Grid.ID = machGrps[j].ToString();
//Grid.AutoGenerateColumns = false;
Grid.AllowSorting = false;
Grid.CellSpacing = 2;
Grid.ForeColor = System.Drawing.Color.White;
Grid.GridLines = GridLines.None;
Grid.Width = Unit.Percentage(100);
Grid.Style.Add(HtmlTextWriterStyle.Overflow, "Scroll");
Grid.ShowHeader = true;
DataTable taskTable = new DataTable("TaskList7");
taskTable.Columns.Add("MachineID");
DataRow tableRow = taskTable.NewRow();
tableRow["MachineID"] = machID[i];
taskTable.Rows.Add(tableRow);
Grid.DataSource = taskTable;
Grid.DataBind();
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
}
Sorry for VB.Net :
Select Case e.Row.RowType
Case DataControlRowType.Header
e.Row.Cells(0).Visible = False
End Select
Edit: C#
if (e.Row.RowType == DataControlRowType.Header){
e.Row.Cells(0).Visible = False;
}