Count of number of columns using rowdatabound - c#

I need to find count of total number of columns in a gridview inside the event rowdatabound .Is there any way for it.
Below is my few code:
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
e.Row.Cells[3].Controls.Add(lnkView);
}

You can cast sender to GridView and get the count.
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(!(sender is GridView))
return;
GridView gridView = (GridView) sender;
var colCount = gridView.Columns.Count;
//Your code
}

Not sure why you want this, but Cells is an array of the cells in this row, so to get the total number of columns:
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
var colCount = e.Row.Cells.Count;
}

Related

Row count of nested GridView is always zero

Parent grid-view is gvAgreement.
Child grid-view is gvProducts.
Code used :
protected void gvAgreement_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string AgreementId = gvAgreement.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvProducts = e.Row.FindControl("gvProducts") as GridView;
gvProducts.DataSource = GetData(string.Format("SELECT dbo.Agreement.*, dbo.Agreementlist.*, dbo.Store.*, dbo.Agreementlist.Agreement_ID AS agreid FROM dbo.Agreement INNER JOIN dbo.Agreementlist ON dbo.Agreement.Agreement_ID = dbo.Agreementlist.Agreement_ID INNER JOIN dbo.Store ON dbo.Agreementlist.ProID = dbo.Store.Pro_ID WHERE (dbo.Agreementlist.Agreement_ID = '{0}')", AgreementId));
gvProducts.DataBind();
int count = gvProducts.Rows.Count;
Session["countgrid"] = count;
}
}
protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (Convert.ToInt32(Session["countgrid"].ToString()) == 1)
{
string message = "alert('Agreement at least must have one product');";
ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "alert", message, true);
}
}
I tried to define count as global value but it also gives zero.
When / how do you databind the child grid? You should do it inside the OnRowDataBound event of the master grid as, presumably, its contents rely on the row of the master grid in which it is located.
protected void gvAgreement_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string AgreementId = gvAgreement.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvProducts = e.Row.FindControl("gvProducts") as GridView;
if(gvProducts != null)
{
gvProducts.DataSource = [some query or data source here]
gvProducts.DataBind();
}
int count = gvProducts.Rows.Count;
Session["countgrid"] = count;
}
}
EDITED AFTER COMMENTS
You shouldn't store the row count in a session variable. You don't need to, because the place where you want to make use of it (in the RowCommand event), you can actually find out what it is.
Something like:
protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
var gvProducts = e.Row.FindControl("gvProducts") as GridView;
if (gvProducts != null && gvProducts.Rows.Count < 1)
{
string message = "alert('Agreement must have at least one product');";
ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "alert", message, true);
}

Gridview index out of range when rows are returning

I want to change the CSS styling of the first row in a gridview:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow firstRow = hoursReportGridView.Rows[0];
firstRow.CssClass = "firstRow";
}
}
I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection.
In every instance there are multiple rows returned so I don't understand the issue
'
I assume the first row exists in GridView.Rows after RowDataBound. So you can access it afterwards. So i would use DataBound instead. Note that you also set the first row on every row since RowDataBound is triggered for every row in the grid.
protected void hoursReportGridView_DataBound(object sender, EventArgs e)
{
if(this.hoursReportGridView.Rows.Count > 0)
hoursReportGridView.Rows[0].CssClass = "firstRow";
}
Another option is to use GridViewRow.RowIndex
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowIndex == 0)
e.Row.CssClass = "firstRow";
}
}
Try this:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 0)
{
e.Row.CssClass = "firstRow";
}
}
Why do you want to do that on the databind for every row?
Just do it in Page_Load:
// Run this after any binding calls, obviously
if(hoursReportGridView.Rows.Count > 0)
{
hoursReportGridView.Rows[0].CssClass = "firstRow";
}

How can i find Last row in a gridview on RowDataBound

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
**if (e.Row.RowIndex >= gridview1.PageSize) // ROW FOOTER TOTAL**
{
e.Row.BackColor = System.Drawing.Color.Firebrick;
e.Row.ForeColor = System.Drawing.Color.White;
}
}
This code works sometimes, someone can help me
DM,cheers
You can try to find the last column in the PreRender event
protected void grid_PreRender(object sender, EventArgs e)
{
GridViewRow row = grdAlert.Rows[grdAlert.Rows.Count - 1];
// do stuff with your row
}
If you just need to change the style of the footer you can use
<asp:GridView ID="grid" runat="server" FooterStyle="your style"></asp:GridView>
A grid view doesn't appear to have a row count until it's finished binding each row. So, another thought:
Can you determine the number of rows from the datatable that the gridview is binding to, then store that in a variable for use later?
you can find last row like this
GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
or use this
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grid = (GridView)sender;
if(e.Row.RowIndex == (grid.Rows.Count - 1))
{
//last row
}
}
}
It seems that you want to detect the footer row in RowDataBound since you have commented //ROW FOOTER TOTAL, you just have to check for the DataControlRowType.Footer:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
// here it is
}
}
Otherwise you could compare the RowIndex with the row of the undrlying DataItem:
DataRowView row = (DataRowView)e.Row.DataItem;
if (e.Row.RowIndex == row.DataView.Count - 1) ; // last row
In your page load method set colors as below
protected void Page_Load(object sender, EventArgs e)
{
//code what you currently have ....
// add below code after that
GridViewRow row = GridView1.Rows.Count-1;
row.BackColor = System.Drawing.Color.Firebrick;
row.ForeColor = System.Drawing.Color.White;
}

How to add a numeric order to the GridView using RowDataBound?

I am a new ASP.NET developer and I am trying to change the value of the first cell in each row in the GridVie by giving it a number starting from 1. It means that I want to add order to the list of things that will be displayed in the GridView.
I am using GridView RowDataBound method but now I don't know to set the limit of the for loop in it. Could anyone help me with this?
My code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
for(int i=1, i<; i++)
e.Row.Cells[0].Text = i;
}
}
If your requirement is "change the value of the first cell in each row", it would be more like:
private int _rowIndex=0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells[0].Text = _rowIndex.ToString();
_rowIndex++;
}
}
You can try this, it will return of cell count:
int cells = ((TableRow) (e.Row)).Cells.Count;

How to bind tooltip on datagrid rows?

I'm trying to bind datagrid rows with tooltip. In which event should i write the code? The row created event doesnt hold my data to bind and returning blank. The code reference is given below:
protected void gdvActionItemView_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].ToolTip = e.Row.Cells[2].Text;
if (e.Row.Cells[2].Text.Length > 100)
{
e.Row.Cells[2].Text.Substring(0, 100);
}
}
Please help.
You can right Row Databound event such as:
protected void grdUserClone_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int colIndex = 0; colIndex < e.Row.Cells.Count; colIndex++)
{
e.Row.Cells[colIndex].Attributes.Add("title", e.Row.Cells[colIndex].Text);
}
}
}
Found the answer. It should be written under RowDataBound.

Categories