add data in the header dynamically - asp.net

I have a grid in which in creating a header dynamically, it contains total,
I'm getting the total from the query and storing it in the hidden field, but value is stored in the
(e.Row.RowType == DataControlRowType.DataRow)
but the header is getting created in
(e.Row.RowType == DataControlRowType.Header)
how to assign the value in the header text

if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = StartNum; i <= EndNum; i++)
{
e.Row.Cells[j + 1].Text = "Week No : " + i.ToString();
j++;
}
}

Related

display number of rows with certain value in gridview asp.net

I used the following code to display the total number of rows in gridview
Label1.Text = "Total Number of Rows: " + e.AffectedRows.ToString();
Now I have a column [NewColumn] with two values, "Yes" and "Null", how can I know the number of rows with the value "YES", and display as "The Number of YES rows: [rows with YES]/[total rows]"?
You can use the RowDataBound event for that. In there check the column NewColumn for the correct value and increment the totals.
int totalRowsWithYes = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//check the column value and increment
if (row["NewColumn"].ToString() == "YES")
{
totalRowsWithYes++;
}
}
}

edit in datarow bound

i have gridview with many col some of col will sum and display in total col using rowdatabound and it works only in view, but the problem when i try to edit using edit statement that gridview have this error appear (Troubleshooting exceptions: system.nullreferenceexception)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string val1 = e.Row.Cells[13].Text; //Gets the value in Column 1
string val2 = e.Row.Cells[14].Text; //Gets the value in Column 2
string val3 = e.Row.Cells[15].Text; //Gets the value in Column 3
string val4 = e.Row.Cells[16].Text; //Gets the value in Column 4
Label lblTotal = (Label)e.Row.Cells[12].FindControl("Label1"); //
float _val1, _val2, _val3, _val4;
float.TryParse(val1, out _val1);
float.TryParse(val2, out _val2);
float.TryParse(val3, out _val3);
float.TryParse(val4, out _val4);
float sum = _val1 + _val2 + _val3 + _val4;
lblTotal.Text += sum.ToString();
}
}
You need to check if the DataRow is in edit mode.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//row is in edit mode
}
else
{
//a normal row
}
}

How to count the no of records in gridview which have some particular data in a coloumn

How to count the no of records in Gridview which have some particular data in a column
name result
======== ======
krishna pass
sanjay pass
ajay fail
out put needed in grid view - above Gridview already present,according to that grid i have to make another grid to count results
result no
====== =====
pass 2
fail 1
in data row bound , i calculated
protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
int oldPos = Convert.ToInt32(pr["oldposition"]);
int newPos = Convert.ToInt32(pr["newposition"]);
GVKeywordReport.HeaderRow.Cells[3].Text = txtfrmdate.Text;
GVKeywordReport.HeaderRow.Cells[4].Text = txtEndDate.Text;
GVKeywordReport.HeaderRow.BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[5].BackColor = ColorTranslator.FromHtml("#FFFFFF");
if (oldPos == newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF950E");
e.Row.Cells[6].Text = "No Change";
nc= nc+1;
}
else if (oldPos > newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FFFFCC");
e.Row.Cells[6].Text = "Improved";
imprv= imprv+1;
}
else
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF0000");
e.Row.Cells[6].Text = "Decreased";
decrs=decrs+1;
}
// e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#7DA647");
}
txt_TargetReached.Text = "0";
txtDecreased.Text =Convert.ToString(decrs);

How to change the color of a gridview cell based on criteria?

This is my gridview output. The Column and row will be binded dynamically.
Anand R 4:4:18 NULL 6:34:52 8:25:16 NULL
Arunkumar S 8:37:31 NULL 9:1:42 8:48:27 NULL
Bharathi R 6:12:24 NULL 8:45:40 11:39:12 12:27:26
Gnanaguru V 6:32:20 NULL 5:35:56 3:50:20 NULL
Ilayaraja K 7:37:30 NULL 10:1:15 8:58:43 NULL
Imran Khan S 7:46:48 NULL 12:15:4 NULL NULL
I need output as if the cell value is greater than 8:30 hours means i need some color and below than 8:30 the cell should be in some color and for the null value the original remain same. So i need to loop through gridview rows and columns dynamically to check the condition. I am using following code in gridview rowdatabound but am not getting exact output..
foreach (GridViewRow row in gvshowreport.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
if (i != 0)
{
string value = row.Cells[i].Text;
if (value != " ")
{
string[] values = Regex.Split(value, ":");
int k = Convert.ToInt32(values[0]);
int m = Convert.ToInt32(values[1]);
int min = k * 60;
int add = min + m;
if (add <= 510)
{
e.Row.Cells[i].BackColor = System.Drawing.Color.LightGreen;
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
}
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.PaleGoldenrod;
}
}
}
}
First time my loop is running perfectly it taking the gridview first row and cell values. While next time loop running it again taking the same first row cell values. I think i have did mistake looping through the rows and columns...
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell cell in e.Row.Cells)
val = cell.Text;
}
I have use this code to achieve my result.. Thanks for everyone posting
as your code, you should replace "e.Row.Cells[i]" with variable "row"
We would change the color of cell in Row_dataBound Event of Gridview
When a single record bind with a gridView Row_dataBound event fires
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Your Cell Control Find, than change the color according to condition
}
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx
Please mark it answer if it helps you

Retain textbox values while Gridview Paging

On page index i am not able to hold the given value in the TextBox ,i have tried different logics but nothing is giving a correct result
here is Code
protected void gvViolationCodes_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
//to rebind the data based on changed page index
violationCodePresenter.GetViolationCodesList(Convert.ToInt32(ddlStatus.SelectedValue), Convert.ToInt32(Session["LanguageID"]));
gvViolationCodes.PageIndex = e.NewPageIndex;
gvViolationCodes.DataBind();
RePopulateValues();
}
and the Method to store previous values is as follows
private void RememberOldValues()
{
DataTable dt = new DataTable();
dt.Columns.Add("row_index");
dt.Columns.Add("edited_value");
foreach (GridViewRow gvr in gvViolationCodes.Rows)
{
TextBox tb = (TextBox)gvr.FindControl("txtSeqNo");
HiddenField hf = (HiddenField)gvr.FindControl("HiddenField1");
if (tb.Text != hf.Value)
{
DataRow dr = dt.NewRow();
dr["row_index"] = gvr.RowIndex;
dr["edited_value"] = tb.Text;
dt.Rows.Add(dr);
}
}
if (dt.Rows.Count == 0 && Session["retain"] == null && Session["page-index"] == null)
{
Session["retain"] = null;
Session["page-index"] = null;
}
else if (dt.Rows.Count > 0 && Session["retain"] == null && Session["page-index"] == null)
{
Session["retain"] = dt;
Session["page_index"] = gvViolationCodes.PageIndex;
}
else if (Session["retain"] == null && Session["page-index"] == null)
{
Session["retain"] = dt;
Session["page_index"] = gvViolationCodes.PageIndex;
}
}
Use ViewState object to hold textbox values across page postback which is occurring because of paging in gridview.

Resources