I want to take LangId value in RowDataBound function. How to do this?
<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// need LangId
ImageButton imgBtn = (ImageButton)e.Row.FindControl("imgBtnDelete");
imgBtn.Visible = false;
}
}
There are a couple of ways to do it. Maybe more.
<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />
protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string langId = e.Row.Cells[columnIndex].Text; // one of the ways
string langId2 = DataBinder.Eval(e.Row.DataItem, "LangId").ToString(); // one of the other ways
}
}
You can get it by this:
string str = e.Row.Cells[CloumnIndexOfYourBoundField].Text;
ColumnIndexOfYourBoundField means if your column is first column than its index is 0 and if its second than its 1 and so on.
The data object is available as e.Row.DataItem at that time. You just need to cast it as the appropriate type.
var myItem = (MyType)e.Row.DataItem;
// myItem.LangId now available
.ASPX file:
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" AlternateText="Edit"
CommandArgument='<%# Eval("LangId") %>' CommandName="DeleteLedger" ToolTip="Delete"
ImageUrl="~/App_Themes/DefaultClient/images/Delete.png" />
</ItemTemplate>
.CS file:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataRowView dataRow = ( DataRowView ) e.Item.DataItem;
string strLangId = dataRow["LangId"].ToString();
DataTable dtData1 =
objAccountTypeBAL.ChkLedgerRelation(Convert.ToInt64(strLangId ), objSession.BranchId);
if (dtData1.Rows.Count > 0)
{
ImageButton img = (ImageButton)item["Delete"].Controls[0];
img.Visible = false;
}
}
Using dynamic type you can access the fields in your record:
if (e.Row.RowType == DataControlRowType.DataRow)
{
dynamic data = e.Row.DataItem;
int LangId = data.LangId;
// do your code here
}
protected void GrdEmplistFromAtt_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("onmouseover", "MouseEvents(this, event)");
e.Row.Cells[0].Attributes.Add("onmouseout", "MouseEvents(this, event)");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_GrdCode = (Label)e.Row.FindControl("lblGrdCode");
}
}
Related
I have a grid view and need to bind IsActive field.But from the database it comes as 1 or 0.
Error shows System.InvalidCastException: 'Specified cast is not valid.'
Grid
<asp:BoundField DataField="IsActive" HeaderText="Status">
<ItemStyle Width="200px" />
</asp:BoundField>
Code
protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((int)DataBinder.Eval(e.Row.DataItem, "IsActive") == 1))
{
e.Row.Cells[12].Text = "Active";
}
else
{
e.Row.Cells[12].Text = "Inactive";
}
}
}
Try grabbing the underlying data item first and then checking for it.
protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView = null;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item
rowView = (DataRowView)e.Row.DataItem;
// Make sure we can parse and compare what we get.
if (int.TryParse(rowView["IsActive"].ToString(), out int isActive) && isActive == 1)
{
e.Row.Cells[12].Text = "Active";
}
else
{
e.Row.Cells[12].Text = "Inactive";
}
}
}
I have a gridview as shown below and i want to hide a column named FREQ_BAND
,second column in button click named btnPmtCalculation which is out side gridview .How to do this ? i reached till this code but not able to proceed
<asp:gridview id="gvSpectrum" runat="server" headerstyle-cssclass="HomeGridHeader"
allowpaging="True" rowstyle-wrap="false" autogeneratecolumns="False" cssclass="table table-striped table-bordered"
cellpadding="4" datakeynames="FEES_CALC_FORMULA,BAND_ID,BAND,FREQ_BAND,SPECTRUM_ID" horizontalalign="Left" showfooter="true"
pagesize="10" onpageindexchanging="gvSpectrum_PageIndexChanging" onrowcommand="gvSpectrum_RowCommand">
<Columns>
<asp:BoundField DataField = "sl_num" HeaderText="SN" />
<asp:BoundField DataField = "FREQ_BAND" HeaderText="Frequency Band" />
<asp:BoundField DataField = "BW" HeaderText="Bandwidth / Quantity" />
<asp:BoundField DataField = "RANGE" HeaderText="Range" />
</Columns>
</asp:gridview>
<asp:button id="btnPmtCalculation" runat="server" text="Generate Permenant Sepctrum Invoice" onclick="btnPmtCalculation_Click">
Code I did as shown below
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
//Button btn = (Button)sender;
////Get the row that contains this button
//GridViewRow gvr = (GridViewRow)btn.NamingContainer;
foreach (GridViewRow row in gvSpectrum.Rows)
{
if (row.RowType == DataControlRowType.Header)
{
row.Cells[1].Visible = true;
}
}}
You can try this:
OnRowCreated
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[columnIndex].Visible = false;
}
If you don't prefer the hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText.
protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)UsersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Email")
.SingleOrDefault()).Visible = false;
}
OnButtonClick
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[Index].Visible = false;
}
Simple, just hide the column in the button click.
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[i].Visible = false;
}
you can change your code with this:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
bool pemenant = true;
int spectrum_id = 0;
int columnIndex = 1;
gvSpectrum.Columns[columnIndex].Visible = false;
}
'columnIndex' The column number is for hide it
or if you want hide with column Header Text Use this code:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
foreach(DataControlField col in gvSpectrum.Columns)
{
if (col.HeaderText == "Email")
col.Visible = false;
}
}
Try this:
protected void btnPmtCalculation_Click(object sender, EventArgs e)
{
gvSpectrum.Columns[columnindex].Visible = false;
//OR
gvSpectrum.Columns["columnname"].Visible = false;
}
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 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;
}
}
}
I would like to change the background color of all the rows with value for "active" column unchecked in a GridView. I tried the following way but it doesn't work:
protected void GdvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox chkItem = null;
foreach (GridViewRow grRow in GdvDetails.Rows)
{
if (grRow.RowType == DataControlRowType.DataRow)
{
chkItem = (CheckBox)grRow.Cells[6].FindControl("active");
if (chkItem.Checked )
{
grRow.BackColor = Color.Red;
}
}
}
}
The error message is "Object reference not set to an instance of an object."
Your code doesn't work because you need to do it on RowDataBund
protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//Paste your code here.
}
}
And add a handler for RowDataBound as so:
<asp:gridview id="CustomersGridView"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
You need to handle RowDataBound event instead of DataBound event, so put you in RowDataBound event as below:
In aspx page, after added the event the code will look like:
<asp:gridview id="GdvDetails"
onrowdatabound="GdvDetails_RowDataBound"
runat="server">
</asp:gridview>
In code behind(.cs):
protected void GdvDetails_RowDataBoundd(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkItem = (CheckBox)e.Row.FindControl("active");
if (chkItem.Checked)
{
GdvDetails.SelectedRow.BackColor = Color.LightGray;
}
}
}
So this WORKS :)
protected void GdvDetails_DataBound(object sender, EventArgs e)
{
CheckBox chkItem = null;
foreach (GridViewRow grRow in GdvDetails.Rows)
{
if (grRow.RowType == DataControlRowType.DataRow)
{
chkItem = (CheckBox)grRow.Cells[6].FindControl("CkbActive");
bool bl = chkItem.Checked;
if (bl == false)
{
grRow.BackColor = Color.LightGray;
}
}
}
}
And the aspx file has
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID= "CkbActive" Checked ='<%# Bind ("active") %>' Enabled="false" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
in the GridView GdvDetails.
i didn't use the code under onrowdatabound because its an event for each row and i thought it will iterate unnecessarily. And this works... just in case anyone else is fumbling like me?!! thx for all the help