display number of rows with certain value in gridview asp.net - 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++;
}
}
}

Related

DataTable: Check a column for existing value

I have two DataTables, DataTable1 sends some of its values to DataTable2. What I want to do is if DataTable1 values already exists in DataTable2, the values will not add to DataTable2.
I have here values from DataTable1
protected void ASPxGridView1_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
{
if (e.ButtonID != "ADD") return;
int id = e.VisibleIndex
int DeliveredQty = Convert.ToInt32(ASPxGridView1.GetRowValues(id, "Delivered Qty"));
int InventoryID = Convert.ToInt32(ASPxGridView1.GetRowValues(id, "InventoryID"));
Now I want to check the DataTable2 values if there's an existing InventoryID in the rows.
This is what I ended up with
int id = InventoryID;
DataTable dt = DataTable2;
DataRow[] dr = dt.Select(id.ToString());
if (dr == null)
{
"if it does not exist, values will be addded"
}
else
{
"prompt user that values exist"
}
What's the correct way to do this?
You can use Linq to check for a value in a DataTable column
bool valueExists = dt.AsEnumerable().Any(x => x.Field<string>("ColumnName") == "abcd");

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

Editing Data Before It Is Displayed In GridView

So basically I have data being pulled from a database for which I want it to display in a gridview. Unfortunately, the days of the week are stored as integers so that monday is equal to 0 and tuesday is 1 etc etc.
Basically, how do I go about changing that data as it is being outputted so that it converts the number to the correct day of the week.
I have got a grid view as follows and at the moment I have it set up with onrowdatabound :
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Width="721px"
onrowdatabound="GridView_RowDataBound"
>
Then the code behind for GridView_RowDataBound is :
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
I assume that I just need to put the right code in there. However, for some reason I can't do anything to a single cell only to every single cell in a particular column. So I could change every cell in one column to bold but can't individually change a cell. I'm sure there is a way but can't fathom how to do it!
Any help would be much appreciated!
Thanks
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int days = (int)GridView1.Rows[e.Row.RowIndex].Cell[2].Text;
if (days == 0) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Monday";
if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Tuesday";
if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Wednesday";
if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Thursday";
if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Friday";
}
}
You can try this.
if (e.Row.RowType == DataControlRowType.DataRow)
{
string days = e.Row.Cells[2].Text;
if (days == "0") e.Row.Cells[2].Text = "Monday";
if (days == "1") e.Row.Cells[2].Text = "Tuesday";
if (days == "2") e.Row.Cells[2].Text = "Wednesday";
if (days == "3") e.Row.Cells[2].Text = "Thursday";
if (days == "4") e.Row.Cells[2].Text = "Friday";
}

Resources