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;
}
Related
I have created a paginated grid view which contains check boxes for multiple selection.But while looping through the data-row in the grid-view only last pages row values only I am able to get.I know it's because of the pagination.
My code is below
protected void btnSaveItemMapping_Click(object sender, EventArgs e)
{
grdCustomers.AllowPaging = false;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
if (myCheckBox.Checked == true)
{
Label lblCustomerCode = (Label)gvrow.FindControl("lblCustomerCode");
//INSERT TO DATABSE
}
}
grdCustomers.AllowPaging = true
}
Keeping check box checked after changing page index like below code.
protected void grdCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
savechkdvls_cst();
grdCustomers.PageIndex = e.NewPageIndex;
if (Session["AllCustomers"] != null)
{
lstAllCustomers = (List<CustomerMaster>)Session["AllCustomers"];
}
grdCustomers.DataSource = lstAllCustomers;
grdCustomers.DataBind();
chkdvaluesp_cst();
}
private void chkdvaluesp_cst()
{
ArrayList usercontent = (ArrayList)Session["chkditems_customers"];
if (usercontent != null && usercontent.Count > 0)
{
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
int index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
if (usercontent.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
myCheckBox.Checked = true;
}
}
}
}
private void savechkdvls_cst()
{
ArrayList usercontent = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
bool result = ((CheckBox)gvrow.FindControl("chkBoxBrandCustomers")).Checked;
// Check in the Session
if (Session["chkditems_customers"] != null)
usercontent = (ArrayList)Session["chkditems_customers"];
if (result)
{
if (!usercontent.Contains(index))
usercontent.Add(index);
}
else
usercontent.Remove(index);
}
if (usercontent != null && usercontent.Count > 0)
Session["chkditems_customers"] = usercontent;
}
Any help will be appreciated.
I have filled listBox with data and assigning it to gridview but it doesn't. I verified by setting count of list to variable and it shows 3, perfect but after assigning it to gridview, the count of gridview shows 0. Why ?
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count; //returns 3
grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count; //return 0
}
}
Solved
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count;
//grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
//grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count;
ListItemCollection lstTempFeatures = ListBoxFeatures.Items;
DataTable dTempFeatures = new DataTable();
dTempFeatures.Columns.Add("ID");
dTempFeatures.Columns.Add("FeatureName");
foreach (ListItem lstItem in lstTempFeatures)
{
DataRow dr = dTempFeatures.NewRow();
dr["ID"]= lstItem.Value;
dr["FeatureName"] = lstItem.Text;
dTempFeatures.Rows.Add(dr);
}
grdViewTemporaryFeatures.DataSource = dTempFeatures;
grdViewTemporaryFeatures.DataBind();
mdlTemporaryFeatures.Show();
}
I have a requirement where i need to add column names and rowdatabound values dynamically .I has managed to get Column names dynamically .According to my need i want values to be displayed in the form of checkboxes in the rows of the grid either in checked form or unchecked based on condition but here every thing is coming in unchecked formate ..
I am using .net membership Role table...
Here is my code for gridview Dynamic column allocation..
protected void BindGridviewData()
{
var role = from MembershipUser u in Membership.GetAllUsers()
select new
{
User = u.UserName,
Role = string.Join(",", Roles.GetRolesForUser(u.UserName))
};
DataTable dTable = new DataTable();
string[] rolesarr = Roles.GetAllRoles();
// add column for user name
dTable.Columns.Add("Username", typeof(string));
// then add all the roles as columns
Array.ForEach(rolesarr, r => dTable.Columns.Add(r));
List<string> tempfield = new List<string>();
foreach (DataColumn column in dTable.Columns)
{
string ColName = column.ColumnName;
tempfield.Add(ColName);
}
for (int i = 0; i < tempfield.Count; i++)
{
string tempfieldname = tempfield[i];
TemplateField temp = new TemplateField();
if (i == 0)
{
temp.HeaderText = tempfieldname;
tempfieldname = string.Empty;
}
else {
temp.ItemTemplate = new MyTemplate();
temp.HeaderText = tempfieldname;
tempfieldname = string.Empty;
}
GridView1.Columns.Add(temp);
}
Now Here i am checking values to be checked or uncked..
foreach (MembershipUser u in Membership.GetAllUsers())
{
DataRow dRow = dTable.NewRow();
dRow[0] = u.UserName;
string[] roles = Roles.GetRolesForUser(u.UserName);
dRow[1] = roles.Contains("Admin") ? true : false;
dRow[2] = roles.Contains("DPAO User") ? true : false;
dRow[3] = roles.Contains("GeneralUser") ? true : false;
dTable.Rows.Add(dRow);
}
GridView1.DataSource = dTable;
GridView1.DataBind();
}
Upto here values are coming fine in dTable but once flow goes to rowdatabound event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
On rowdatabound event flow automatically moves to custom control class which is ...
public class MyTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
CheckBox chk = new CheckBox();
chk.ID = "chk";
container.Controls.Add(chk);
}
}
And from here all checkboxes are displaying as unchecked ...
Please guys help me ..
Thanks in advance...
Do it in this way. check where the column is true false in the rowDatabound event and the add control here only.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = new CheckBox();
if(e.Row.Cells["Column Name"].toString == true ){
cb.Checked = true;
}else{
cb.Checked = false;
}
e.Row.Cells["Column Name"].Controls.Add(cb);
}
}
I have a method called BindItems() and this method gets the values to a DataTable and then a GridView is bound to it.
Then I have an itemtemplate in that gridview which contains a drop down, this drop down gets populated from 1 -14 from code behind under Gridview_RowDataBound, now I need to figure a way to get the Quantity value in the DataTable on the other function "BindItems()" and for each Row in the gridview so a SelectedValue = datatable["Quantity"] or something.. how can I do this?
protected void BinItems(int myId)
{
//this data table contains a value "Quantity"
DataTable dt = MyClass.getItems(myId);
uxGrid.DataSource = dt;
uxGrid.DataBind();
}
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//Some how i need to do a SelectedValue = datatable where field = Quantity for each row
}
You need to access the current-being-bound-row's DataItem, which in your case, is a DataRowView (since you are binding to a DataTable):
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//here goes the "magic"
DataRowView dRow = e.Row.DataItem as DataRowView;
if(dRow != null)
{
Myddl.SelectedValue = dRow["Quantity"].ToString();
}
}
you should outside the DataTable dt and then youll be able to access it in the Function
i've a grid on my page i need to refresh gridview add and delete new record but its not?
here is the code:
Add Row To GridView:
private void AddClientToGrid()
{
int clientID = int.Parse(ddlClient.SelectedValue);
int clientTypeID = int.Parse(ddlClientType.SelectedValue);
ClientsAllCDO client = new ClientsBL().ClientsAllSelectByIDAndClientTypeID(clientID, clientTypeID);
List<ClientsAllCDO> clientList = new List<ClientsAllCDO>();
clientList = GetClientsFromGrid();
clientList.Add(client);
gvClient.DataSource = clientList;
gvClient.DataBind();
}
Delete Code:
protected void btnDeleteClient_Click(object sender, EventArgs e)
{
LinkButton btnDeleteClient = sender as LinkButton;
int rowIndex = int.Parse(btnDeleteClient.Attributes["RowIndex"]);
if (Request.QueryString["BailiffID"] == null)
{
gvClient.DeleteRow(rowIndex);
}
else
{
int bailiffID = int.Parse(FormCrypto.Decrypt(Request.QueryString["BailiffID"]));
GridViewRow gvRow = gvClient.Rows[rowIndex];
int clientTypeID = int.Parse(((Label)gvRow.FindControl("lblClientTypeID")).Text);
int clientID = int.Parse(((Label)gvRow.FindControl("lblClientID")).Text);
gvClient.DeleteRow(rowIndex);
new BailiffClientsBL().BailiffClientDelete(clientID, bailiffID, clientTypeID);
}
}
Thanks alot...
You need to rebind the grid to the datasource:
//delete row from the database
GridView1.DataSource = SomeDataRetrievalMethod(); //retrieve the data from the database
GridView1.DataBind();