I am looping 2 Datagrids and getting the IndividualID's (PrimaryKeys) of the person(s) that are selected. I have debugged this and the loops are working fine. Howver when I call my Update Method from my Table Adapter, nothing seems to happen. It does not update. Here is my code. Not sure what I'm doing wrong:
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in gvAdminCustomer.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkitemSelectorCustomers");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvAdminCustomer.DataKeys[row.RowIndex].Value));
// GlobalVar.oIndividualID = oIndividualID;
foreach (GridViewRow r in gvReassignCustomers.Rows)
{
CheckBox chkBox = (CheckBox)row.FindControl("chkitemSelectorAllManagersandSalesman");
if (chkBox != null && chkBox.Checked)
{
int oNewParentID = Convert.ToInt32((gvReassignCustomers.DataKeys[r.RowIndex].Value));
individualTableAdapter ind = new individualTableAdapter();
//Individual ind = new Individual();
ind.reassign_Individual(oIndividualID, oNewParentID);
}
}
gvAdminCustomer.DataBind();
}
}
}
Is it updating the database record and not reflecting on the grid? If that is the case, then it looks like it is just rebinding the grid with the datasource it used prior to the loop. You'll need to refresh your dataset and then rebind:
gvAdminCustomer.DataSource = YourDataSource;
gvAdminCustomer.DataBind();
Good Luck!
Related
I am trying to delete selected rows from gridview in button click event but I am getting the below error:
Collection was modified; enumeration operation might not execute.
Code:
protected void btnRemoveUser_Click(object sender, EventArgs e)
{
DataTable yourDataTable = GetDataTable(Studentlist);
//CheckBox chkbx;
foreach(GridViewRow oneRow in Studentlist.Rows)
{
CheckBox chkbx = (CheckBox)oneRow.FindControl("ckbxID");
foreach (DataRow rowDT in yourDataTable.Rows) **//In this line throwing the exception**
{
if (chkbx.Checked == true)
{
yourDataTable.Rows.Remove(rowDT);
}
}
}
Studentlist.DataSource = yourDataTable;
Studentlist.DataBind();
}
Note: Studentlist is the gridview ID.
You can not remove item from collection while performing foreach on it for details visit this.
For now you can use easy technique to delete items.
for(int i = 0; i< yourDataTable.Rows.Count; i++)
{
if (chkbx.Checked == true)
{
yourDataTable.Rows.RemoveAt(i);
}
}
I have a project that using dropdownlist for choices.When check checkbox1 dropdown automatically bind data from database using table1 and when I check checkbox2 dropdown automatically binding data from database using table2.I do not want to use get data by using any button .How can I do that .Please help me.
here is code by using button:
public void LokasyonDoldur()
{
birimBUS = new BirimBUSV1();
List<BirimVO> birimVO = new List<BirimVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
birimVO = birimBUS.LokasyonlariGetir();
foreach (var item in birimVO)
{
items.Add(new ListItem(item.BirimAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
public void BirimleriDoldur()
{
PoliklinikBUS poliklinikBUS = new PoliklinikBUS();
List<PoliklinikVO> poliklinikVO = new List<PoliklinikVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
poliklinikVO = poliklinikBUS.Poliklinikler();
foreach (var item in poliklinikVO)
{
items.Add(new ListItem(item.PoliklinikAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
protected void BtnLokasyon_Click(object sender, EventArgs e)
{
if (ChckLctn.Checked == true && ChckBrm.Checked==false)
{
LokasyonDoldur();
}
else if (ChckLctn.Checked == false && ChckBrm.Checked == true)
{
BirimleriDoldur();
}
else
{
}
Button1.Visible = true;
BtnLokasyon.Visible = false;
}
protected void DrpChcs_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
KirilimId = Int32.Parse(DrpChcs.SelectedValue);
BPolikilinikID= KirilimId;
}
but I do not want to use this one.
ohh its another language. its hard to read. but what you basicly have to do is check which checkbox is checked in the page load and then load the dropdown based on what is loaded.
something like this. (I have typed it from my head so its not like copy-paste but you get the idea)
page_load
{
if(checkbox1.checked)
{
dropdown.dataitems = items1;
dropdown.databind();
return;
}
if(checkbox2.checked)
{
dropdown.dataitems = items2;
dropdown.databind();
return;
}
}
YOu can call the Button1_click event from the Dropdown list selected index changed event like this
Button1_Click(Button1,new EventArgs());
and in this you can hide that button from the page and in code behind you are calling the same function
OR
You can refactor the code in a seperate function from the button click event and call that function in the selected index changed event.
Please let me knwo if I misunderstood your question
Thanks
I have a GridView which is binded with DataTable for the datasource, when there is no records in the underlying datasource i show up the EmptyDataTemplate with Two DropDown (State and City) and based on the State i fills up City.
Now my problem is till i have some records inside the Datasource it works fine, but as soon as i delete the last remaining record from the datasource, it throws error, it switches to the emptydatatemplate but it could not find the States Dropdownlist (based on which i am filling city dropdown). But once i refresh the page it works fine.
I don't understand which event to grab to place the codes
Here's what i am doing
Below function gets the stateid and fills the cities based on it
private void FillCitiesByStateId()
{
drpCity = grdLocationView.Controls[0].Controls[0].FindControl("drpCitiesAdd") as DropDownList;
drpState = grdLocationView.Controls[0].Controls[0].FindControl("drpStatesAdd") as DropDownList; // Fetches the States DropDown from EmptyDataTemplate.
if (drpState != null)
{
objCity.StateId = Convert.ToInt32(drpState.SelectedValue); //It always throws error on this line.
drpCity.DataSource = objCity.GetCitiesByStateId();
drpCity.DataTextField = "Name";
drpCity.DataValueField = "Id";
drpCity.DataBind();
}
}
Below is the code i which is used to bind the records in dropdowns
protected void grdLocationView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (dt.Rows.Count == 0)
{
FillCitiesByStateId();
}
else
{
if (e.Row.RowType == DataControlRowType.Header)
{
drpCity = e.Row.FindControl("drpCities") as DropDownList;
drpState = e.Row.FindControl("drpStates") as DropDownList;
objCity.StateId = Convert.ToInt32(drpState.SelectedValue);
drpCity.DataSource = objCity.GetCitiesByStateId();
drpCity.DataTextField = "Name";
drpCity.DataValueField = "Id";
drpCity.DataBind();
}
}
}
Can anyone provide any suggestion whats going wrong with it
Make sure you are only calling the FillCitiesByStateId() for appropriate row types. Maybe you're calling it for a footer row?
DataControlRowType Enumeration
instead of:
if (dt.Rows.Count == 0)
{
FillCitiesByStateId();
}
try:
if (e.Row.RowType == DataControlRowType.EmptyDataRow)
{
FillCitiesByStateId();
}
i have a gridview in which i am using checkbox in each row. i am trying to access checkbox of each row and trying to find out which checkboxes have been checked.buut when i try to run the below code.the condition always stands to be false and the inner if condition is never reached by the code.kindly help me.thanks in advance.
protected void btn_3id_Click(object sender, EventArgs e)
{
string str = "";
string srr = "";
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked==true)
{
if (str == "")
{
str = GridView1.Rows[i].Cells[0].Text.ToString();
}
else
{
srr = str + "," + GridView1.Rows[i].Cells[0].Text.ToString();
}
}
}
Session["Card_id"] = str;
Response.Redirect("ID.aspx");
}
The code looks fine.
The problem could be you are binding the gridview at page load.
Try grid binding in the following section of page load
if(!Page.IsPostBack)
{
//code to bind the gridview
}
I can only guess that you are binding your gridview on each page load without checking PostBack. That is causing the checkbox to loose its current state. So where you are assigning the DataSource to the Gridview , Check for PostBack like:
if(!Page.IsPostBack)
{
GridView1.DataSource = yourDataSource;
GridView1.DataBind();
}
also you can do some minor improvements in your code like your check:
if(chk.Checked == true)
can be replaced as:
if(chk.Checked) //Since it returns a bool value.
You can omit multiple string variables for concatenation. Its better if you use StringBuilder, (See why it is better) so your code would be:
protected void btn_3id_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked==true)
{
sb.Append() GridView1.Rows[i].Cells[0].Text.ToString();
}
}
Session["Card_id"] = sb.ToString();
Response.Redirect("ID.aspx");
}
if(!Page.IsPostBack)
{
//
}
Postback plays important role in cs file. If you are clearing values on page load , you will null values of checkbox.
You code is fine.
Just try to do this...
Currently I have a Button Click event which takes the DataKey from a row in a Datagrid (via a checkbox) and assigns it to an INT variable to Update an Individual in a databsse. Everything works fine when I only check 1 checkbox and run the Update. However I need the User to have the option to select all checkboxes and take all those Datakeys and assign it to the same variable
INT oNewParentID
Like I said evrything works fine when only 1 checkbox is Selected. I guess I'm asking how to get an Uknoiwn amount of Datakeys or all DataKeys selected by all Checkboxes and store them in the Variable above to run the Update for all Individuals Selected. Here is my Button Click Event so far which is working for 1 checkbox selected:
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow row in gvSalesmanCustomers.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));
foreach (GridViewRow r in gvSalesmanByManager.Rows)
{
CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
if (chkBox != null && chkBox.Checked)
{
int oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));
Individual ind = new Individual();
ind.ReassignIndividual(oIndividualID, oNewParentID);
chkBox.Checked = false;
cb.Checked = false;
}
}
}
}
}
And her is my Update Stored Procedure :
CREATE DEFINER=`root`#`%` PROCEDURE `reassign_Individual`(
IN oIndividualID int(11),
IN oNewParentID int(11)
)
BEGIN
Update intelliair.individual
set ParentID = oNewParentID
Where IndividualID = oIndividualID;END
Re-organized code.
Added break to second foreach assuming that there's no reason to ReasignInvidual multiple times just to assign it to the last selected manager.
protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
List<int> ids = new List<int>();
foreach (GridViewRow row in gvSalesmanCustomers.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
if (cb != null && cb.Checked)
{
int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));
ids.Add(oIndividualID);
cb.Checked = false;
}
}
int oNewParentID = 0;
foreach (GridViewRow r in gvSalesmanByManager.Rows)
{
CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
if (chkBox != null && chkBox.Checked)
{
oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));
chkBox.Checked = false;
break; //no reason to go with the same logic to next records
//if you want to uncheck all the checkboxes continue with specific code just for that purpose
}
}
if (ids.Count > 0 && oNewParentID > 0)
foreach(var id in ids)
{
Individual ind = new Individual();
ind.ReassignIndividual(id, oNewParentID);
}
}