I have 2 listboxes. One contains all the department which are unassigned and other listbox contains all the department which is assigned to a particular person. Now I want to add and delete departments using insert and delete query. The query will be performed on 1 table only ie Assigned Department(listbox2) and by clicking the SAVE button. I have done the insert part but unable to handle the delete part. I have seen few examples but they dont have DB event in them.
protected void Save_Click(object sender, EventArgs e)
{
DataTable dt = GetData2();
bool found = false;
foreach (RadListBoxItem item in RadListBox2.Items)
{
found = false;
foreach (DataRow dr in dt.Rows)
{
if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
{
found = true;
label1.Text = "Add a New Group to the ListBox";
}
}
if (found == false)
{
Item(item.Text, item.Value);
}
This is what I am trying to do. I want to handle insert and delete event on Save button.
I figured out the solution so I am posting it in here.
foreach (DataRow dr in dt.Rows)
{
found = false;
foreach (RadListBoxItem item in RadListBox2.Items)
{
if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
{
found = true;
}
}
if (found == false)
{
//delete here
}
}
}
I use the RadListBox's Transferred event. When the Transferred event fires Items look at the destination listbox's id and determine if the items were moved to the unassigned or assigned ListBox. Once you know the destination you can execute a query to add or remove rows from your database.
In any case, your Query will be dependent on your schema. In my situation I had a lookup table that determined if the item was attached.
rlistAvailable_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
{
if (e.DestinationListBox.ID == "rlistAttached")
{
foreach (Telerik.Web.UI.RadListBoxItem li in e.Items)
{
//do insert query using li.Value
}
}
else
{
foreach (Telerik.Web.UI.RadListBoxItem li in e.Items)
{
//do delete query using li.Value
}
}
}
Related
I have seen a few posts similar to this issue, but I haven't come up with my answer so, I thought I would float it again to see what comes up...
I am using ExcelDNA to integrate an API with Excel using C#.NET. I have a DataGridView, and I would like to check items that already exist on a list.
The following code works when tied to a button-click event, but NOT when the code is called in a method.
private void SelectAllItems()
{
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];
check.Value = check.TrueValue;
}
}
I am running into the same issue elsewhere, too:
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];
string hid = row.Cells["Hid"].Value.ToString();
if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
{
check.Value = check.TrueValue;
}
}
I've been researching this for a few hours, coming up completely empty. Any help would be greatly appreciated.
You can do this by setting the value to true. You do not need to cast to a checkbox cell.
private void SelectAllItems()
{
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
// This will check the cell.
row.Cells["selectItem"].Value = "true";
}
}
foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
string hid = row.Cells["Hid"].Value.ToString();
if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
{
row.Cells["selectItem"].Value = "true";
}
}
I have a gridcontrol which is binded to a list using a BindingSource.
invoice_GroupListBindingSource.Datasource= _list.
GridControl.DataSource = invoice_GroupListBindingSource
Now there is one checkedit column (let's call it Invoice_Bool). The functionality requirement is when user select anyone of he column all the datarows should be updated and select column for them should also be selected.
Foloowing is the code I tried:
private void invoice_GroupListBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
Data.InvoiceGroupList.InvoiceGroupInfo current;
if (invoice_GroupListBindingSource.Current == null)
{
current = null;
}
else
{
current = ((Data.InvoiceGroupList.InvoiceGroupInfo)invoice_GroupListBindingSource.Current);
}
if (current.Invoice_Bool)
{
foreach (var item in _invoice_list)
{
item.Invoice_Bool = true;
}
this.invoice_GroupListBindingSource.DataSource = _invoice_list;
}
}
With this I am getting an updated list with all the Invoice_Bool true when user selects any of them (as required), however in the front-end(in the grid) all the bool values are still unselected. Any idea how to update the grid also when data source changes.. ideally it should be done automatically but I am not sure why it is not updating.
Im looking to programmatically delete from a GridView.
I have implemented the gridview's OnRowDeleting="Delete_Record" event handler, and this is called when the user clicks the delete link on the GridView, but as the row doesn't seem to be selected the code which does the delete does not work.
To test further I can click select, to select the row (commenting out the code behind the select) then if I click delete the code runs fine.
Is there a way to have the row selected as the first step in the Delete_Record?
GridViewRow row = gvResults.SelectedRow;
string id = row.Cells[1].Text;
ASB_DataDataContext db = new ASB_DataDataContext();
var delCase = from dc in db.Inputts
where dc.counter == Convert.ToInt32(id)
select dc;
foreach (var nCase in delCase)
{
db.Inputts.DeleteOnSubmit(nCase);
db.SubmitChanges();
}
Since you are using the event you should be having the required index as part of the GridViewDeleteEventArgs e which should be like say e.RowIndex
you can then access your needed parameter as grid.Rows[e.RowIndex].Cells[1].Text
You can achieve by the code shown below
public void GridView1_Delete_Record (Object sender, GridViewDeleteEventArgs e)
{
string id = gridView1.Rows[e.RowIndex].Cells[2].Text;
ASB_DataDataContext db = new ASB_DataDataContext();
var delCase = from dc in db.Inputts
where dc.counter == Convert.ToInt32(id)
select dc;
foreach (var nCase in delCase)
{
db.Inputts.DeleteOnSubmit(nCase);
db.SubmitChanges();
} Message.Text = "";
}
Set db.Inputts as DataSource of your datagrid. Just add next two lines after your code:
datagridView1.DataSource = null;
datagridView1.DataSource = db.Inputts;
Setting datasource to null is for clearing previous data from datagrid;
I have a listview that is loaded with a list of objects that contain an attribute called AssigneeView which holds the date that the entry was opened. The listview's ItemTemplate has a label named "lblHeader". What I want to do is loop through the ListView.Items and check each element's AssigneeView attribute, if it is null, I want to set the lblHeader.Text to be bold (indicating it is unread).
I want to create a method that takes an attribute from the Object in the Items list called ticketID and lookup whether or not the AssigneeView field is null for that field and return a bool. So it would look something like
ForEach item in listview.Items
if(IsUnread(item.datamember.ticketID)) then
item.lblHeader.MakeBold
else
item.lblHeader.MakeNotBold
I'm not 100% on how to dig into the telerik control to get what I need to do this. Any suggestions?
UPDATE:
here's where I am at the moment:
using (var client = new QUTIService.QSVCClient())
{
var data = client.SearchTickets(this.myGuid, txtSearchString.Text, 100, chkSearchClosed.Checked).ToList();
lsvResultTickets.DataSource = data;
lsvResultTickets.DataBind();
if (data.Count == 0)
{
lblStatus.Text = "No tickets found.";
}
else
{
foreach (var item in lsvResultTickets.Items)
{
var obj = item.DataItem as QT.FullTicket;
if (TicketIsUnread(obj.OriginalTicket.TicketID))
{
//???
}
}
}
}
What you will need to do is check AssigneeView in the RowDataBound event and then set lblHeader accordingly. RowDataBound is called for each row in you datasource as it is added to the GridView.
if (e.Row.RowType == DataControlRowType.DataRow) {
if (DataBinder.Eval(e.Row.DataItem, "AssigneeView") == null) {
//Set bold
} else {
//Set normal
}
}
Ok, it turns out that I just had to drill down one more level. I didn't need to pull out another method to do the check for me. I handled this is in the item loaded event handler, here's what I ended up with:
protected void ResultItem_DataBound(object sender, RadListViewItemEventArgs e)
{
var dItem = e.Item as RadListViewDataItem;
var dObj = dItem.DataItem as QT.FullTicket;
//if no read date, mark as unread (bold)
if (dObj.AssigneeView == null)
{
var headerLabel = e.Item.FindControl("lblHeader") as Label;
headerLabel.Style.Add("Font-Weight", "Bold");
headerLabel.Style.Add("Color", "Orange");
}
}
I have a dropdown in my webpage, which always returns the value 0 as the selected index no matter whichever item the user selects. I have populated the dropdown using a DB query. And I am populating in on Page_Load method in my page. The code shown below does the specified work: int danceid;
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDanceDropDown();
}
}
private void PopulateDanceDropDown()
{
DataTable dt = new DataTable();DataRow row = null;
dt.Columns.Add("Did", Type.GetType("System.Int32"));
dt.Columns.Add("DName", Type.GetType("System.String"));
var dancer_dance = (from dd in context.DANCER_AND_DANCE
where dd.UserId == dancerId
select new
{
Value = dd.DanceId,
Text = dd.DanceName
}).ToList();
foreach (var dndd in dancer_dance)
{
row = dt.NewRow();
row["Did"] = dndd.Value;
row["DName"] = dndd.Text;
dt.Rows.Add(row); dances.DataSource = dt;
dances.DataTextField = dt.Columns[1].ToString();
if (!IsPostBack)
{
dances.DataBind();
}
}
protected void changeIndex(object o, EventArgs e)
{
danceid = dances.SelectedIndex;
}
protected void dropthedance(object o, EventArgs e)
{
int danceIDFromDropDown = danceid;
var dancer_dance = from dd in context.DANCER_AND_DANCE
where dd.DanceId == danceIDFromDropDown
select dd;
foreach (var dndd in dancer_dance)
{
context.DANCER_AND_DANCE.DeleteOnSubmit(dndd);
}
try
{
context.SubmitChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
The line int danceIDFromDropDown = danceid; in the method dropthedance always has the value 0. Pleaseeeeeeeeeeeeee help someone
Are you sure that you want to be using the index as the ID? Typically, you're going to want to set the actual ID from your database as the DataValueField and then you can grab the value that way.
But I also noticed that you grab the index and place it into a variable on the indexchanged event and then you try to use that value in a different method. I'm assuming that danceid is an attribute somewhere not shown here. At any rate, the value isn't persisting through postbacks. Instead of trying to store it in a variable like you would on a desktop application, try adding EnableViewState="True" to your dropdown control. Then get that index on your submit handler directly. Or if you really want to store it in a variable, then try persisting the value of that variable by storing it in the session or caching it, then pull from that cache/session variable when it comes time to actually use the value.
But again, it might be better practice to place the danceid in the listitem object itself. Just the though of basing IDs on item indexes makes me shudder, especially when you populating the list from a database, because what happens when you add a new item to that list in the library and then try to sort them by name... then your indices become useless.
Replace
int danceIDFromDropDown = danceid;
with
int danceIDFromDropDown = dances.SelectedIndex;
It may work.