append fields dynamically at runtime based on user input in c# - c#

In c# how would I best handle this scenario, a form contains a large number of checkboxes 100+ which map to columns/fields in the data store, and the user can select any of them. Depending on those they choose when they submit the form I will generate a csv output of the data but only showing the fields(columns) they have requested.
list myData = getAllTheData
var fieldstheychose = "name, mileage, address"
foreach (var item in mydata)
{
var somedata = new StringBuilder();
// make decision to include field or not
somedata.Append(item.name.formatvalue() + ",");
// make decision to include field or not
somedata.Append(item.mileage.GetCSVEncodedValue() + ",");
// make decision to include field or not
somedata.Append(item.address.ToDisplayString() + ",");
// make decision on 100+ fields that might be in the fieldstheychose list depending on their choices
}
return somedata;
I need to at runtime decide which fields will be appended above but I wanted to avoid 100+ if blocks to decide weather to append the data.
I did look at dynamic objects and expando objects but I'm not sure it helps in this scenario.
Any views appreciated.

List<ListItem> selected = new List<ListItem>();
foreach (ListItem item in YourCheckBoxListID)
if (item.Selected)
selected.Add(item);
This will store the ListItems that have been checked assuming you are using a CheckBoxList. From there you can iterate through your selected list and convert the entries into proper csv format.

Give the name of the checkboxes same to what you have in the list mydata. Loop through the list and find your check-boxes using the findControl() function to get the checkbox checked Boolean value. Add it to the list.

Related

C#: How to store stuff in dynamic arrays?

I am writing a program where the users can type any number and amount of denomination + one overall input. The program has to get this input and store them somewhere to be able to use them for further usage such as calculations.
Could you please tell me how to define this first part and how to access the stored info?
It's not ideal to use an Array for any dynamic data.
//You can add to a list dynamically
List<int> denominations = new List<int>();
//Values below are hardcoded for demonstration purposes
denominations.Add(34);
denominations.Add(12);
denominations.Add(95);
//Iterate over list
foreach (int item in denominations)
{
Console.WriteLine(item);
}

Creating a new form - Sharepoint

So I have the code as such:
private void newAddFormForMembers(SPList list, SPWeb web)
{
list = web.Lists["MemberRecords"];
string url = string.Format("_layouts/createform.apsx", web.ServerRelativeUrl, list.RootFolder.Url);
var form = web.GetFile(url);
if (form != null)
{
list.DefaultNewFormUrl = url;
list.Update();
}
}
I have used SharePoint 2010 designer to go and grab the existing html for the creatform form when adding items to a particular list. I added in two new fields, first and last name. The list contains a member name, I removed this field from the create form because:
When I hit submit to add the item to the list, first and last name need to format them selves into "lastname, first name". Now I could submit the form back to my self - How do I do that? - and then do my string manipulation How do i get the values form a field? - and then push the information to the list How do I do that? but then I have anew issue
Edit will look the exact same as add, first and last name fields, how would I take the information from the list and populate the new edit form, particularly first and last name fields, keeping in mind that in the list they are in the format of "lastname, firstname"?
This is all being done programatically
Instead of change html newForm for the list try to write an Event Receiver
Instead of delete field from the newForm try to hide columns (You can do that programmatically or using external tool such as Sharepoint Manager
Adding fields to the new/edit form and grab value on postback event I think is not simple/possible (via javascript you can access onPreSave where you can manipulate item values before postback, here you can write some jscript for save values into field but on edit is hard to split values).
You can: create two column on your list FirstName and LastName, plus a third field FullName (hidden for new/edit form) and with an EH on Added/Updated you can write FullName = LastName + ", " + FirstName or without EH you can create a calculated Field.

Programmatically Checking DevExpress CheckedComboBoxEdit Items

I have a CheckedComboBoxEdit that's bound to a TableAdapter that populates it with a list of items.
I have a separate query that returns a dataset that lists the items that need to be checked.
I need to iterate through the CheckedComboBoxEdit items to check them as needed.
How can I make the CheckedComboBoxEdit reflect the data from the query which returns a list of items that need to be checked?
I'm using C# in Visual Studio 2010 with DevExpress 10.2.9.
Any help on this would be greatly appreciated, and any other solutions to this issue would be great too.
Items state of the CheckedComboBoxEdit tied to its EditValue. You can check items by setting an appropriate editor value: a list of values (each item has the value and display text) delimited with a separator sign. The separator sign is specified via the RepositoryItemCheckedComboBox.SeparatorChar property.
Here is how to
checkedComboBoxEdit1.Properties.SeparatorChar = ';';
// Set the edit value, assuming you have items named "one",and "two"
checkedComboBoxEdit1.SetEditValue("one; two");
Here is the complete example
Short code snippet.
string str = "first;second";
string[] array = str.Split(';');
char separator = checkedComboBoxEdit1.Properties.SeparatorChar;
string result = string.Empty;
foreach (var element in array){
result += element + separator;
}
checkedComboBoxEdit1.SetEditValue(result);

How can I modify a repeater to not show data for every row bound to it?

I have a stored proc that returns 4 rows in a table. Each row corresponds to a day a person is staying at a hotel, so when I bind a repeater to it, 4 rows show up. Two of the rows are the same room and the other two are the same room. I want to display credit card information, but not for each individual room, only for every set of rooms that is the same, so it would look something like this:
12/7/2011 Room 1
12/8/2011 Room 1
Credit Card Info
12/7/2011 Room 2
12/8/2011 Room 2
Credit Card Info
I don't want to combine Room information and credit information into a one row each. How can I get this to format the way I want it to?
Currently I am displaying Room Information in an ItemTemplate and inside the ItemTemplate is a tr and td's.
You basically have two options: you can pre-process the data so that what is bound to the repeater represents what you want to present to the user - DJ KRAZE has shown one way to do that.
The other option would be to add an ItemDataBound event handler and manipulate the data and/or the current row at that point.
One drawback of the 2nd approach is that you need access to the prior row in order to make your decision - that may complicate things, especially if you have any paging of the results.
//Create either a HashTable()
//or a Dictionary<int, string[]> I would personally use a Dictionary<>
//create an instance of what ever collection you want to use or array
//List<string>
//Dictionary<int, string[]>() using the room number as the key
// you would add the values to either the List<> or a String[] array or a Dictionary
keey in mind that if you want to create a dynamic string[] array and you do not know how many elements you are going to add just use a List<> or Dictionary<int,string>
and you can always convert that object using .ToArray
so for example
List<string> lstRoomList = new List<string>();
string[] arrayRoom = {};
if you have a DataReader for example and you are doing something like
while (yourdataReader.Read())
{
// here you can decide how you are going to work with the data you
lstRoomList.Add(datareader["roomnubmer"] + " " + datareader[CreditCarInfo];
}
at the end you could assign the values of that list to a dynamic array
arrayRoom = kstRoomList.ToArray(); I would really need to see what you are using and how you are returning the StoredProc results..

how do I populate 2nd listbox by selecting from 1st listbox using jquery?

I have two questiosn.
1)
I have a list box which gets populated depending on what I select in my 1st drop down.
The data is retrieved using jQuery.get.
The code for generating the list looks like this:
(...)
foreach (DataRow row in dt.Rows)
{
strList.Append("<option value='" + row["id"] + "'>" + row["enhetsnavn"] + "</option>");
}
I append the result to my drop down list with the following code:
var schoolsList = $("#schoolSelect");
jQuery.get(
site + "jQueryFunctions.ashx",
{
county: county, schoolType: schoolType, instance: 'getSchoolsByCounty' },
function(data) {
schoolsList.append(data);
}
);
This works great the first time. The problem is that if I select something new from the 1st ddl, it gets added to the 2nd list thus not replacing existing items.
(the list just keeps getting longer and longer).
How do I replace the list items with the new ones?
2)
If I remember correctly, populating the 2nd drop down list using jQuery will not bind the data. And if it's not bound, I'm not able to retrieve value / data using jQuery.
I think I had to use jQuery.live or something?
use
schoolsList.html(data);
in order to replace the content of the element instead of append() which obviously will just append the new data to it.
As for the second part I don't fully understand what data you want to bind here. The jQuery.live method is for event listeners which you don't need right now, I guess.
Your code doesn't include anything where you want to retrieve any data. If it is a form and you send it, the data will be transmitted if this is what you are concerned about.

Categories