I have a label control on my page and beneath the label control there is a Gridview.The value that exists in the label control also exists in the Gridview. The label control has value like this
3244|Yellow Ink| Test Link
In the gridview, I have a value 3244 too
3244 yello Ink Test Link
3255 Green Link Test2
I want the row Index of 3244 in my code behind as soon as the page loads. Is their any way to do it.
Not knowing what type of data source you have, one way of doing it would be with LINQ like this:
protected void Page_Load(object sender, EventArgs e)
{
// Fetch the text from your label. (I'm assuming that you have only one label with the text "3244|Yellow Ink| Test Link".
string text = Label1.Text;
// Find the first row or return null if not found.
var resultRow = GridView1.Rows.Cast<GridViewRow>().FirstOrDefault(row =>
{
// Get the id (that I'm guessing is the first (0-index) column/cell)
var id = row.Cells[0].Text;
// Return true/false if the label text starts with the same id.
return text.StartsWith(id);
});
if (resultRow != null)
{
var index = resultRow.RowIndex;
}
}
A shorter version would look like this:
var resultRow = GridView1.Rows.Cast<GridViewRow>()
.FirstOrDefault(r => text.StartsWith(r.Cells[0].Text));
protected void btnJobAppSelected_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewJobApplications.Rows)
{
int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
LinkButton btnJobAppSelected = (LinkButton)GridViewJobApplications.Rows[rowIndex].FindControl("btnJobAppSelected");
}
}
In your page load after populating gridView, you can do that like this:
String searchValue = "3244 ";
int rowIndex = -1;
foreach (GridViewRow row in GridViewID.Rows)
{
if (row.Cells[0].Text.ToString().Equals(searchValue))
{
rowIndex = row.RowIndex;
break;
}
}
int YourIndex = rowIndex;
Related
I have a Gridview and a RepositoryItemGridLookUpEdit in that GridView
I want to show a CustomDisplayText in the RepositoryItemGridLookUpEdit
private void rgluePerson_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
var person = rgluePerson.GetRowByKeyValue(e.Value) as Person;
var name = person.Name;
var surname = person.Surname;
e.DisplayText = name + ", " + surname;
}
}
The problem is that the person name depends of another cell in the same row (in the main Gridview), and i don't know how to get the current's Gridview row being processed (current row doesn't work since i need the row being processed in the moment)...... i can't use a gridView event cuz it will change the cell value, but i want to change the Text value.
Does anyone knows how to do that?
You cannot get the row being processed by the CustomDisplayText event because there are no such fields or properties that contains current row. You can only use this event for focused row. For this your must check if sender is type of GridLookUpEdit:
private void rgluePerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
if (!(sender is GridLookUpEdit))
return;
var anotherCellValue = gridView1.GetFocusedRowCellValue("AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}
For not focused rows you can use only ColumnView.CustomColumnDisplayText event:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column.ColumnEdit != rgluePerson)
return;
var anotherCellValue = gridView1.GetListSourceRowCellValue(e.ListSourceRowIndex, "AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}
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"]);
}
}
trying to pass parameter from a label in gridview, only the label text from the first row are passed.
not sure what is missing.
protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
}
}
}
You are breaking loop with Response.Redirect You need to put the do Response.Redirect out side loop to set all value, you also need to concatenate value of lbl_taskID all rows instead of over writting.
protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string taskIds = string.Empty;
if (e.CommandName == "edit")
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
if(Session["TaskID"] != null)
taskIds = Session["TaskID"].ToString();
Session["TaskID"] = taskIds + lbl_taskID.Text + ",";
}
}
Response.Redirect("~/tasks_edit.aspx");
}
}
If only the first row is used, then why you using foreach loop? You can simply find the control of the 0th row.
if (e.CommandName == "edit") //this makes sure that you have a row
{
Label lbl_taskID = (Label)GridView1.Rows[0].FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
If you mean label text from currently editing column then, take the index of the editing column from the CommandArgument and get the label
if (e.CommandName == "edit") //this makes sure that you have a row
{
int index = Convert.ToInt32(e.CommandArgument); //currently editing row index
Label lbl_taskID = (Label)GridView1.Rows[index].FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
How come you have lbl_taskID in every row? In your foreach loop your doing-
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
You are actually only taking the value of lbl_taskID present in your first row. The next rows will not have the same element again. Your coding is wrong. You will need to name your labels in each row with some thing like this- label0, label1,... then in your code you can do-
int i=0;
foreach(GridViewRow row in GridView1.Rows)
{
Label xyz = (Label)row.FindControl("Label"+i);
Session["TaskID"+i] =xyz.Text; //to have unique session variables
i++;
}
Response.Redirect("~/tasks_edit.aspx"); // you should redirect only when you come out of the loop
You are using a foreach loop, but you are using the loop to assign a single value to a single Session variable. So what do you expect?
However, i would assume that your last row is assigned not the first.
You need to put Response.Redirect("~/tasks_edit.aspx") outside of the loop since it will abort the current request. You might want to assign the row which is currently in edit-mode:
foreach (GridViewRow row in gridView1.Rows)
{
if (row.RowState == DataControlRowState.Edit)
{
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
break;
}
}
Response.Redirect("~/tasks_edit.aspx");
Side-note: you don't need to check the DataControlRowType since GridView.Rows only returns rows with DataControlRowType.DataRow anyway(unlike RowDataBound-event).
Edit: Instead of RowCommand i would use the click event of the LinkButton:
protected void EditLink_Clicked(Object sender, EventArgs e)
{
// get the LinkButton reference
LinkButton link = (LinkButton) sender;
// get the GridViewRow reference via NamingContainer
GridViewRow row = (GridViewRow) link.NamingContainer;
Label lbl_taskID = (Label) row.FindControl("lbl_taskID");
Session["TaskID"] = lbl_taskID.Text;
Response.Redirect("~/tasks_edit.aspx");
}
I have used the auto generated EDIT button from gridview wizard and it is working.
sorry for your time waste.
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;
This is my code in c#
protected void ibtCopiar_Click(object sender, ImageClickEventArgs e)
{
GridViewRow grdrows = DGPlanilla.Rows[0];
TextBox tb1 = (TextBox)grdrows.FindControl("TextBox1");
string valor = tb1.Text;
if (valor != null || valor != "0")
{
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
grdrow.Cells[5].Text = valor;
}
}
}
this my code for the button
when i debug i see that the value i have in the firts box is pass to the other textboxes in the column, but when it dysplay the page onle the first box show the value i have enter. the other texboxes donĀ“t change.
If the other cells contain textboxes as well, you will need to find them and set their text value.
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
TextBox toDisplay = (TextBox)grdrow.FindControl("TextBox1");
toDisplay.Text = valor;
}