How to access multiple rows in a gridview - c#

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;

Related

RowIndex of Gridview Error in asp.net

I am implementing the bulk delete functionality with the help of Checkbox. But when I call the ID like below
string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
I get the error as
System.EventArgs does not contain a definition of RowIndex.
I dont know why it is happening. Please see my code for your reference:-
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in grdUser.Rows)
{
CheckBox chkDelete = (CheckBox)grdUser.FindControl("chkDelete");
if (chkDelete.Checked)
{
string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
}
}
}
Do let me know what changes I have to make
You should add the gvRow.RowIndex as stated by Sandeep.
Then you have to bind your gridview somewhat like this.
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in grdUser.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
CheckBox chkDelete = (CheckBox)gvRow.FindControl("chkDelete");
if (chkDelete.Checked)
{
string Id = grdUser.DataKeys[gvRow.RowIndex].Value.ToString();
DeleteRecordByID(Id);
}
}
}
//Bind your Gridview here
}
Let me know if it works or not
you can Use Below Code
for (int i = 0; i < grdUser.Rows.Count; i++)
{
//Your logic and use grdUser.DataKeys[i].Value.ToString();
for delete
}

asp.net gridview if cell row value is equal then

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"]);
}
}

populating DropDownList in a gridview with array elements

I have been trying to Populate a DropDownList in my gridview with array elements . The array consists of the column names from another gridview .The array element seems to get the column names from its source but i can not figure out how to give that to the dropdownlist.Here is my code-:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] excel = new string[250];
DataTable dtt = (DataTable)Session["griddata"]; //griddata is the gridview data from another page
for (int i = 0; i < dtt.Columns.Count; i++)
{
excel[i] = dtt.Columns[i].ColumnName;
}
Session["exceldata"] = excel;
ArrayList mylist= (ArrayList)Session["exceldata"];
DropDownList drd = (DropDownList)GridView2.FindControl("DrdDatabase");
drd.DataSource = mylist;
drd.DataTextField = "GridView";
drd.DataBind();
}
Thanks in Advance :)
Here is the logic which you can use to populate your dropdownlist.
->
Loop at the array.
{
For each item in array
{
add item to dropdownlist.items
}
}
Sorry for not providing the exact code as I dont have .net editor at my disposal right now but you can use the logic to implement it.
regards.
You could simply loop it and add the ListItems programmatically:
DropDownList drd = (DropDownList)GridView1.FindControl("DrdDatabase");
foreach(string colName in mylist)
drd.Items.Add(new ListItem( colName ));
However, are you sure that you find your DropDownList via GridView1.FindControl? I assume you get a NullRefernceException there. Then you need to show us where it actually is.
If it is in a TemplateField of the GridView you should use the RowDataBound event:
private ArrayList ExcelData
{
get {
object excel = Session["exceldata"];
if (excel == null) Session["exceldata"] = new ArrayList();
return (ArrayList)Session["exceldata"];
}
set {
Session["exceldata"] = value;
}
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase");
foreach (string colName in ExcelData)
ddl.Items.Add(new ListItem(colName));
}
}

how to store multiple values of checkbox values in viewstate?

here is the code for my viewstate. but it's only store one value.what i need is it will keep the selected multiple values in the checkbox. this method is to keep/hold the value of check box in gridview of paging situation.
public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox selectBox = (CheckBox)sender;
GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row
GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;
int a = rowSelect.RowIndex;
ViewState["id"] = ID;
}
Try Following
Following Code may help u.
public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox selectBox = (CheckBox)sender;
GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row
GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;
int a = rowSelect.RowIndex;
ArrayList SelecterdRowIndices=new ArrayList();
if(ViewState["SelectedRowIndices"]!=null)
{
SelecterdRowIndices=(ArrayList)ViewState["SelectedRowIndices"];
bool flag=false;
foreach (int i in SelecterdRowIndices)
{
if(i==Convert.ToInt32(ID))
{
flag=true;
break;
}
}
if(!flag)
{
SelecterdRowIndices.Add(ID);
}
}
else
{
SelecterdRowIndices.Add(ID);
}
ViewState["SelectedRowIndices"] = SelecterdRowIndices;
}

getting the rowindex of gridview based on the label control value

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;

Categories

Resources