So basically I am doing a manage packaging item system. The scenario is, when I select from gvSPU, gvFinalised should display the item in database. After that, when I add new item into the gvFinalised, it supposed to keep stacking up the items instead of wiping out the previous record and display the added latest one. Here is the code:
private List<DistributionStandardPackingUnitItems> tempDistSPUI
{
get
{
if (ViewState["tempDistSPUI"] == null)
{
return new List<DistributionStandardPackingUnitItems>();
}
else
{
return (List<DistributionStandardPackingUnitItems>)ViewState["tempDistSPUI"];
}
}
set
{
ViewState["tempDistSPUI"] = value;
}
}
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
int rowNo = int.Parse(e.CommandArgument.ToString());
SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
lblSPUname.Text = SPUname;
List<DistributionStandardPackingUnitItems> templist = new List<DistributionStandardPackingUnitItems>();
templist = tempDistSPUI;
templist = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
gvFinalised.DataSource = templist;
gvFinalised.DataBind();
this.tempDistSPUI = templist;
}
When gvSPU row is selected, it should store all the records into templist and display in gvFinalised.
List<string> prodVariantIDList = new List<string>();
private List<string> SelectedVariantDetailIDs
{
get
{
if (ViewState["SelectedVariantDetailIDs"] == null)
{
return new List<string>();
}
else
{
return (List<string>)ViewState["SelectedVariantDetailIDs"];
}
}
set
{
ViewState["SelectedVariantDetailIDs"] = value;
}
}
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<ProductPacking> prodVariantDetail = new List<ProductPacking>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
When add button is on click, it should append with the record in gridview instead of wiping them off and insert the checked one.
I am using the viewState to store the record between postbacks. However, when I try to add new item into gvFinalised, the previous record which in the templist will disappear and the gvFinalised will be populated with the result from prodVariantDetail list.
Any guide? Thanks in advance.
EDIT
List<DistributionStandardPackingUnitItems> itemList = new List<DistributionStandardPackingUnitItems>();
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
int rowNo = int.Parse(e.CommandArgument.ToString());
SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
lblSPUname.Text = SPUname;
itemList = tempDistSPUI;
itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
gvFinalised.DataSource = itemList;
gvFinalised.DataBind();
this.tempDistSPUI = itemList;
}
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
for (int j = 0; j < itemList.Count; j++)
{
prodVariantDetail.Add(itemList[j]);
}
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
}
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
You have to add the elements of tempDistSPUI into prodVariantDetail before binding prodVariantDetail to gvFinalised.
Here's what I would do using List.AddRange Method:
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
// get the content of tempDistSPUI from ViewState
itemList = this.tempDistSPUI;
// add all elements of itemList to prodVariantDetail
prodVariantDetail.AddRange(itemList);
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
Related
I have a ListView in my Form.
I want to delete a row, but when I select one and delete it, later it comes back and I can't delete from the List where data comes from.
Why is that?
Pontszam pt = new Pontszam();
List<int> pontszamook = new List<int>();
List<string> nevek = new List<string>();
List<string> idk = new List<string>();
List<string> ptList = new List<string>();
int osszSor = 0;
public int sorokSzama()
{
osszSor = pt.select().Count;
return osszSor;
}
private void Ponttablazat_Load(object sender, EventArgs e)
{
osszSor = sorokSzama();
ptList = pt.select();
listScore.Items.Clear();
for (int i = 0; i < osszSor; i++)
{
string[] adatok = ptList[i].Split(';');
idk.Add(adatok[0]);
nevek.Add(adatok[1]);
pontszamook.Add(Convert.ToInt32(adatok[2]));
var row = new string[] { nevek[i], Convert.ToString(pontszamook[i]) };
ListViewItem lvi = new ListViewItem(idk[i]);
lvi.SubItems.AddRange(row);
listScore.Items.Add(lvi);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (listScore.SelectedItems != null)
{
for (int i = listScore.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = listScore.SelectedItems[i];
int indeX = itm.Index;
listScore.Items[itm.Index].Remove();
ptList[indeX] = null;
idk[indeX] = null;
nevek[indeX] = null;
pontszamook[indeX] = -1;
pt.deletetRow(itm.Index);
}
}
}
private void btnDelete_Click(object sender, EventArgs e) { if (listScore.SelectedItems != null) { for (int i = listScore.SelectedItems.Count - 1; i >= 0; i--) { ListViewItem itm = listScore.SelectedItems[i]; int indeX = itm.Index; listScore.Items[itm.Index].Remove(); ptList[indeX] = null; idk[indeX] = null; nevek[indeX] = null; pontszamook[indeX] = -1; pt.deletetRow(itm.Index); } } }
I have a DataList that contains CheckBoxList, and I filter the datalist on selectedindexchanged outside datalist. The problem is when I select value from dropdownlist I can't get the checked values from database
and checkboxlist items count is 0
this is the code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string GroupValue;
if (DropDownList1.SelectedValue.ToString().IsEmpty()) { GroupValue = null; } else { GroupValue = DropDownList1.SelectedValue.ToString(); }
if (pageMode == PageMode.Update)
{
int rowID = int.Parse(GrdUsers.GetRowValues(GrdUsers.FocusedRowIndex, "ID").ToString());
//ConnectionLayer cn = new ConnectionLayer();
//DataTable dt = cn.ExecutQuery("PagesSys", new object[] { 0, DropDownList1.SelectedItem.Text.ToString() });
//dlstPages.DataSource = dt;
//dlstPages.DataBind();
BindOption(rowID);
}
private void BindOption(int UserID)
{
try
{
if (pageMode == PageMode.Update)
{
if (DropDownList1.SelectedItem.Value != "-1")
{
dlstPages.DataSource = ViewState["FunctionOption"];
dlstPages.DataBind();
if (dlstPages.Items.Count > 0)
{
for (int i = 0; i < dlstPages.Items.Count; i++)
{
DataTable dt = new Users().UserPrivilege(UserID, int.Parse(dlstPages.DataKeys[i].ToString()));
if (dt.Rows.Count > 0)
{
dt.PrimaryKey = new DataColumn[] { dt.Columns["OptionId"] };
CheckBoxList chklist = (CheckBoxList)dlstPages.Items[i].FindControl("chkOption");
for (int j = 0; j < chklist.Items.Count; j++)
{
if (dt.Rows.Find(chklist.Items[j].Value) != null)
{
chklist.Items[j].Selected = true;
}
}
}
}
}
}
}
}
catch (Exception ex)
{
}
}
You need to use the arguments to the function to get the dropdown list object, then you can find the selected value. Something like:
DropDownList ddl= (DropDownList)sender;
var value = ddl.SelectedValue.ToString();
Good day, I have a list view that looks like this.
My problem is that the name of the items are overlapping, that name is from the database, do i have to set something in the properties for it not to overlap?
form_load code
private void FormAdhocReport_Load(object sender, EventArgs e)
{
LoadReport();
ToolBar(sRights);
}
LoadReport code:
private void LoadReport()
{
dsReports.Clear();
this.listReports.Items.Clear();
adhoc.AccountRowID = CurrentUserNameRowID;
string sRetXMLValue = adhoc.get_sp_Reports_View_Owner();
string XMLDOC = sRetXMLValue;
ReadXMLData(XMLDOC);
ListReportData();
//int count = listReports.Items.Count;
}
ListReportDataCode:
private void ListReportData()
{
try
{
// Get the table from the data set
DataTable dtable = dsReports.Tables[0];
// Clear the ListView control
this.listReports.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["ReportName"].ToString());
lvi.ImageIndex = 0;
lvi.SubItems.Add(drow["RowID"].ToString());
listReports.Items.Add(lvi);
}
}
}
catch { }
}
Don't mind this codes: string sRetXMLValue = adhoc.get_sp_Reports_View_Owner();
string XMLDOC = sRetXMLValue;
ReadXMLData(XMLDOC); because they're just for getting the data in the database.
Thanks in advance for your help.
I have a list of integers.5 items max.These 5 integers shows 5 indexes of listbox items.I want to move this items in the first 5 places of this listbox.This listbox is filled from a list with this code
uList.Add(new KeyValuePair<int, string>(item.Id, item.Name));
listBoxHome.DataSource = uList;
listBoxHome.DisplayMember = "Value";
listBoxHome.ValueMember = "Key";
indexes is the list of integers refer to index of items.
game.listBoxHome.BeginUpdate();
for (int i = 0; i < 5; i++)
{
foreach (int PlayingInd in indexes)
{
HomeList.Insert(i, HomeList[PlayingInd]);
HomeList.RemoveAt(PlayingInd);
}
}
game.listBoxHome.DataSource = HomeList;
game.listBoxHome.DisplayMember = "Value";
game.listBoxHome.ValueMember = "Key";
game.listBoxHome.EndUpdate();
I use this code to one form:
public void button7_Click(object sender, EventArgs e)
{
Sub sub = new Sub();
foreach (ListItem item in listBox2.Items)
{
uList.Add(new KeyValuePair<int, string>(item.Id, item.Name));
}
sub.HomeList = uList;
}
And in the other form:
private BindingList<KeyValuePair<int, string>> Homelist;
public BindingList<KeyValuePair<int, string>> GetHomelist
{
get { return Homelist; }
set { Homelist = value; }
}
private void button2_Click(object sender, EventArgs e)
{
BindingList<int> indexes = new BindingList<int>();
foreach (int indexChecked in checkedListBox1.CheckedIndices)
{
indexes.Add(indexChecked);
}
Game game = new Game();
foreach (int PlayingInd in indexes)
{
Homelist.Insert(0, Homelist[PlayingInd]);
Homelist.RemoveAt(PlayingInd + 1);
}
game.GetHomelist = Homelist;
this.Close();
}
solved
How can store name of dynamically created checkbox in a String array when I don't know how many checkbox will user select at runtime.
Say I have 10 dynamic checkboxes and out of 10 user select 6 checkboxes randomly now how can get the name of those selected checkboxes and store them in a String array.
I know how to use event handler on dynamic check box but confused how to declare Straing array when I don't know what will be be size of an array.
Here what I have done till now -
private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
//Label myLabel;
String str = null;
if (c.Checked == true)
{
str = c.Text;
gpBox[gpcount] = new GroupBox();
gpBox[gpcount].Name = "gpBox" + Convert.ToString(count);
gpBox[gpcount].Text = str;
gpBox[gpcount].Location = new Point(5, gpposition);
gpBox[gpcount].AutoSize = true;
this.Controls.Add(gpBox[gpcount]);
aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection);
aAdapter3 = new OleDbDataAdapter(aCommand3);
ds3 = new DataSet();
aAdapter3.Fill(ds3, "app_info");
ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true);
int batch_count = ds3.Tables[0].Rows.Count;
batchCheckBox = new CheckBox[batch_count];
//filling the groupbox with batch code by generating dynamic checkboxes
for (int j=0; j < batch_count; ++j)
{
batchCheckBox[j] = new CheckBox();
batchCheckBox[j].Name = "batch" + Convert.ToString(k);
batchCheckBox[j].Text = ds3.Tables[0].Rows[j][1].ToString();
Console.WriteLine(batchCheckBox[j].Text);
batchCheckBox[j].Location = new System.Drawing.Point(104 * position, 30);
gpBox[gpcount].Controls.Add(batchCheckBox[j]);
batchCheckBox[j].CheckStateChanged += new System.EventHandler(BatchBoxCheckedChanged);
position++;
count++;
Console.WriteLine(batchCheckBox[j].Name);
k++;
}
position = 1;
gpposition += 100;
}
else
{
count--;
this.Controls.RemoveByKey("lbl" + c.Name);
this.Update();
}
}
int total_batch = 1;
string[] batchname;
private void BatchBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox batchBox = (CheckBox)sender;
//Here I want to store name of checkbox in array
if (batchBox.Checked == true)
{
batchname = new String[total_batch];
total_batch++;
}
else
{
}
}
You can try this:
//Gets all checkbox's on the form
List<CheckBox> chks = Controls.OfType<CheckBox>().ToList();
//take only those who is checked, and select only their name property
List<string> names = chks.Where(c => c.Checked).Select(c => c.Name).ToList();
UPDATE
For testing you could print a list of the selected names:
string txt = "";
foreach(string name in names)
{
txt += name+" \n\r";
}
MessageBox.Show(txt);
Thank you everbody
}
list = new List<string>();
}
private void BatchBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox batchBox = (CheckBox)sender;
//Here I want to store name of checkbox in array
if (batchBox.Checked == true)
{
list.Add(batchBox.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach(string prime in list) // Loop through List with foreach
{
Console.WriteLine(prime);
}
}
This is Done