edit in datarow bound - asp.net

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
}
}

Related

Dropdown is not getting selected

When I pass the string value to the dropdown, it is not getting selected. I am not sure why?
I've tried passing the value eg:ddlInitialIncidentType.Items.FindByValue("1").Selected = true; directly which works fine.
protected void btnIncTypeSave_Click(object sender, EventArgs e) {
string value;
if (rbIncTypeY.Checked == true) {
//getting the value number from the Label
value = label.Text;
ddlInitialIncidentType.ClearSelection();
//here I want to select the dropdown with the value number
ddlInitialIncidentType.Items.FindByValue(value).Selected = true;
}
}
Note: I am assigning the value to the label in the below method
function prioritySelection(sender) {
var e = document.getElementById(sender.id);
e = e.value;
if (e == 2 || e == 4 || e == 1 || e == 3)
{
$('#<%=lblInitialIncidentTypeCurrent.ClientID%>').html(e); $find("ContentPlaceHolder1_ContentPlaceHolder2_ModalPopupIncidentTypeChange").show();
}
protected void btnIncTypeSave_Click(object sender,EventArgs e)
{ string value; if (rbIncTypeY.Checked==true) {
value=label.Text; // did you check here that value gets value or not?} }
In your javascript code you have used the ID of the Label control as lblInitialIncidentTypeCurrent where as in your server side code your are using some other Label control.
Replace this
value = label.Text;
with
value = lblInitialIncidentTypeCurrent.Text;
There was some problem by using the Label to store the value.
But by using HiddenField it is solved.
Thanks all for the answers provided.
below is the code for it,
//aspx.cs
<asp:HiddenField ID="hdtest" runat="server" />
protected void btnIncTypeSave_Click(object sender, EventArgs e)
{
string value;
if (rbIncTypeY.Checked ==true)
{
value = hdnIncType.Value;
ddlInitialIncidentType.ClearSelection();
ddlInitialIncidentType.Items.FindByValue(value).Selected = true;
ModalPopupIncidentTypeChange.Hide();
rbIncTypeY.Checked = false;
}
}
//aspx
function prioritySelection(sender) {
var e = document.getElementById(sender.id);
e = e.value;
if (e == 2 || e == 4 || e == 1 || e == 3) {
$('#<%=hdtest.ClientID%>').val(e);
$find("ContentPlaceHolder1_ContentPlaceHolder2_ModalPopupIncidentTypeChange").show();
}
}

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++;
}
}
}

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);

add data in the header dynamically

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++;
}
}

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

Resources