Know the gridview row height in asp.net code behind - c#

I have a GridView and using DataSet to Bind that. And Gridview Height set to autoHeight;
Suppose I have 3 rows in My GridView, After bind I want to fetch the row height of each row.
I know that I can set the RowHeight dynamically by
GridView1.RowStyle.Height = 40;
But I want somthing like my be
int height=GridView1.Rows[0].Height;
I can manage my logic of the above code using for or any.....

If you don't want to use a foreach loop, then you can use a RowDataBound event to find the height of each row.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
ViewState["rowHeightList"] = new List<decimal>();
}
}
if (e.Row.RowType == DataControlRowType.DataRow) {
decimal currentRowHeight = Convert.ToDecimal(e.Row.Height);
List<decimal> rowHeightList = (List<decimal>)ViewState["rowHeightList"];
rowHeightList.Add(currentRowHeight);
ViewState["rowHeightList"] = rowHeightList;
}

Related

how to change the value of a gridview in row data bound event

i want to hide last 4 digits of mobile number in gridview and show last 4 digits as **** . im getting only header value not item template values. how to get mobile values/item values and edit it and assign to grid view?
protected void gvrequests_RowDataBound(object sender, GridViewRowEventArgs e)
{
string Mobile = e.Row.Cells[3].Text;
string securedPhone = Mobile .Remove(6);
string MobileSecured= securedPhone + "****";
e.Row.Cells[3].Text=MobileSecured
}
You need to check the row first that it is DataRow or not like this.
protected void gvrequests_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// your logic will go here
}
}
protected void gvrequests_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color: #87CEFA";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Mobile = e.Row.Cells[3].Text;
string securedPhone = Mobile .Remove(6);
string MobileSecured= securedPhone + "****";
e.Row.Cells[3].Text = MobileSecured;
}
}
RowDatabBound even fired on each row means for header row , data row (alternate row too) and footer row.
So, when you want to manipulate the data, as #Sain suggest, you check that it is datarow then our logic implement.
The same logic is also apply for header and footer, but ideally we should use for the data row only.

How can i get gridview row count

I am trying to get gvProducts row count.
I have tried the below code but it give insufficient result.
protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
if (row.Controls.Count == 1)
{
//my code
}
}
You want total rows in Gridview? Use Count property:
gvProducts.Rows.Count
Update:
To find the rows count of nested gridview, you can use the RowDataBound event of parent gridview:-
protected void gvProductsParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvProducts = (GridView)e.Row.FindControl("gvProducts ");
int count = gvProducts.Rows.Count;
}
}
Please note this event will fire for each row present in your parent gridview this the count will change according to each row.

Display gridview row count based on dropdown selection

I am using this code to take a gridview count and display in a label on page load and works fine.
Page Load:
int rowCount = dtDetails.Rows.Count;
lblTotalRows.Text = rowCount.ToString() + "records found";
I have a dropdown above my gridview and when I select dropdown values the row count have to changed based on the dropdown selected values.
How could I possibly do that in dropdown selected index change
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtGroup = DataRepository.GetGroup(ddlGroup.Text);
gvDetails.DataSource = dtGroup;
gvDetails.DataBind();
//Now how could I possible show the respective row counts in the label
}
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtDept = DataRepository.GetDept(ddlGroup.Text, ddlDept.Text);
gvDetails.DataSource = dtDept;
gvDetails.DataBind();
//Now how could I possible show the respective row counts of both group and
dept row count since they are cascading dropdowns in the label
}
Any suggestions?
I tend to make a SetData() method so all this kind of code is in one place. So in this instance I would:
protected void SetData(DataTable dtGroup)
{
// Bind the data to the grid
gvDetails.DataSource = dtGroup;
gvODetails.DataBind();
// Show row count
if (!dtGroup.Rows.Count.Equals(0))
lblTotalRows.Text = dtGroup.Rows.Count + " records found";
else
lblTotalRows.Text = "No records found";
}
This way you only have one place that does all the 'bindind' so in your Page_Load you can just call this SetData() method and pass in the datatable, and the same on your SelectedIndexChanged.

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

How to change row height on last page of Gridview

On the last page if If i have 1 or 2 items the header and the pager stretch too much.. it all autosizes..
My gridview height is not set, But I set the row property to 30pixels..still doesnt prevent the autosizing..
I was searching for a solution over the net..and the closest solution that i found was that:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(this.GridView1.Rows.Count<this.GridView1.PageSize)//I need here to grab the last page..// do something like e.Row.... and last page.
{
foreach (GridViewRow tt in GridView1.Rows)
{ tt.Height = Unit.Pixel(30); }
}
}
It isnt correct.... what i need to happen, is when the user clicks the last page the height should be modified to to 30 pixels.
Any other ideas on how to prevent autosizing on the last page are welcome!!
To find out if you're on the last page of a paged grid and then change row heights, use the following (tested) code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
e.Row.Height = Unit.Pixel(30);
}
}

Categories

Resources