I am populating a datatable, then binding it to a gridview. Now I'm reading the rows in the grid and coloring the row if value = [x].
The thing when I try to display on the page the row that is colored im getting it duplicated.
Lets say i have colored 1 row but the response.write will be like 100 times the same result.
Below is my code, hope someone can help :
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
// loop over all the Rows in the Datagridview
foreach (GridViewRow row in gv1.Rows)
{
// if condition is met color the row text
if (gv1.Rows[0].Cells[0].Text.ToString() == alert)
{
Session ["fn"] = gv1.Rows[0].Cells[2].Text;
gv1.Rows[0].ForeColor = Color.Red;
}
Response.Write(Session["fn"]);
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
if (e.Row.Cells[0].Text.ToString() == alert)
{
Session ["fn"] = e.Rows.Cells[2].Text;
e.Rows.ForeColor = Color.Red;
Response.Write(Session["fn"]);
}
}
Related
I have a grid view and in this grid some rows have a field called "Department". What I want is a code that reads that fields value and then if it equals a string "Industrial" the row should be hidden and not shown.
I tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (e.Row.Cells[4].Text == "industrial")
e.Row.Visible = false;
}
But it keeps saying (e) is not defined, and there is no such thing as (.Row).
It's GridViewRowEventArgs that have access to e.Row but it's used in RowCreated and RowDataBound events. You can use SelectedRow property of GridView instead.
var gridView = (GridView)sender;
if (gridView.SelectedRow.Cells[4].Text == "industrial")
gridView.SelectedRow.Visible = false;
Updated:
To hide the rows when page is loaded, place the for loop inside the Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
// ...
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[4].Text == "industrial")
GridView1.Rows[i].Visible = false;
}
}
I am using GridView with AutoGenerateEditButton="True" which gives me edit functionality. My problem is, if I want to get cell value in RowEditing event than I can get it by using grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text but if I want to get cell value in RowCancelingEdit event than grdEmpDetail.Rows[e.RowIndex].Cells[2].Text is always empty.
How can I get the Cell value in RowCancelingEdit event?
CODE:
protected void grdEmpDetail_RowEditing(object sender, GridViewEditEventArgs e)
{
//Reset the edit index.
this.grdEmpDetail.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
WorkSampleBusiness.BusinessObject objBL = new WorkSampleBusiness.BusinessObject();
if (!String.IsNullOrEmpty(grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text)) //This gives me current cell value
{
lastName = grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text;
DataBindGrid(objBL.EmployeeDetails(lastName));
}
else
DataBindGrid(objBL.EmployeeDetails(lastName));
}
protected void grdEmpDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
this.grdEmpDetail.EditIndex = -1;
//Bind data to the GridView control.
WorkSampleBusiness.BusinessObject objBL = new WorkSampleBusiness.BusinessObject();
if (!String.IsNullOrEmpty(grdEmpDetail.Rows[e.RowIndex].Cells[2].Text)) // This is always empty
{
lastName = grdEmpDetail.Rows[e.RowIndex].Cells[2].Text;
DataBindGrid(objBL.EmployeeDetails(lastName));
}
else
DataBindGrid(objBL.EmployeeDetails(lastName));
}
Hi all I would like to change cell index based on certain value like shown below ,I have seen many articles in here, but in ASP.NET this is a windows app how can I archive this thanks with a windows desktop app.Please Note the column that I want indexes changed is created dynamically.Thanks
Dynamic column code creation
private void button3_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
adap.Fill(table);
dataGridView1.DataSource = table;
table.Columns.Add("RESULTS").Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),'GOOD''BAD'))
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[7].Value.ToString() == "BAD")
row.Cells[7].Style.ForeColor = Color.Red;
//row.Cells["RESULTS"].Style.ForeColor = Color.Red;
}
}
As you are adding a new column to the datatable table, you need to bind the table to the datagridview (refer this : how to bind datatable to datagridview in c#) and then try to change the color.
Try this
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red; //change the color to what you need
}
UPDATE : To iterate through the DataGridView and check for cell contents in a specific column, you need something like
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[7].Value.ToString() == "BAD")
row.Cells[7].Style.ForeColor = Color.Red;
//row.Cells["RESULTS"].Style.ForeColor = Color.Red;
}
You need to place this piece of code inside an event that is triggered or function that is called after the DataGridView is populated with data.
Here is some sample code demonstrating the desired result
ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="true">
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList al = new ArrayList();
al.Add("open"); al.Add("close"); al.Add("other"); al.Add("open"); al.Add("other"); al.Add("close"); al.Add("open");
this.GridView1.DataSource = al;
this.GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "open")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
}
else if (e.Row.Cells[0].Text == "close")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Black;
}
else
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
}
}
}
I am trying to add the column names of a gridview to a dropdown list.
The problem is when I change the datasource of the gridview, it returns empty.
I am not sure whether the event is working properly, as it does not work with/without it.
I wanted to use the DataBindComplete event, but I could not see it so I tried DataBound instead.
private void BindTable()
{
if (ddTableSearch.SelectedIndex == 0)
{
tblCustomerTableAdapter customerAdapter = new tblCustomerTableAdapter();
GridView2.DataSource = customerAdapter.GetData();
GridView2.DataBind();
}
else if (ddTableSearch.SelectedIndex == 1)
{
tblInvoiceTableAdapter invoiceAdapter = new tblInvoiceTableAdapter();
GridView2.DataSource = invoiceAdapter.GetData();
GridView2.DataBind();
}
else if (ddTableSearch.SelectedIndex == 2)
{
tblEstimateTableAdapter estimateAdapter = new tblEstimateTableAdapter();
GridView2.DataSource = estimateAdapter.GetData();
GridView2.DataBind();
}
}
protected void GridView2_DataBound(object sender, EventArgs e)
{
// Populate dropdown with column names
ddColumnSearch.Items.Clear();
for (int i = 0; i < GridView2.Columns.Count; i++)
{
ddColumnSearch.Items.Add(new ListItem(GridView2.Columns[i].ToString()));
}
}
What am I doing wrong?
The databound event runs for each record in the gridview. So one issue is that each time you are clearing out the items you added before. Another issue is that you need to get the data from the EventArgs instead of from the gridview.columns since those are not there until after all the data is bound. I think all you need to do is get the data from the header row:
protected void GridView2_DataBound(object sender, EventArgs e)
{
// Populate dropdown with column names
if(e.Row.RowType != DataControlRowType.Header) return; //only continue if this is hdr row
ddColumnSearch.Items.Clear();
foreach (TableCell cell in e.Row.Cells)
{
ddColumnSearch.Items.Add(new ListItem(cell.Text));
}
}
I have a gridiview which gives me a output of 20 members in a list. Now i want to pay salary to only specific person and to only to those persons whose checkbox is checked i tried some thing like this a follows :
protected void Button_Click(object sender, EventArgs e)
{
foreach (GridViewRow GVR in GridView.Rows)
{
if (GVR.RowType == DataControlRowType.DataRow)
{
CheckBox c = (CheckBox)GVR.FindControl("MemberCheck");
if (c.Checked)
{
string DividendAmount = GridView.Rows[0].Cells[5].Text;
string MOP = GridView.Rows[0].Cells[4].Text;
}
}
}
}
But the problem is by this code i can access only any one particular row but what if i have selected n rows ???
Don't you mean?
string DividendAmount = GVR.Cells[5].Text;
string MOP = GVR.Cells[4].Text;