I am trying to re define the select query and make it user defined by first listing out all the fields in one checkbox list and grouping them in another and providing a where condition for selected check boxed fields. I am returning a view model that contains the list of all my columns.
I need to know how to populate the list from my view model onto a asp.net checkbox list.
Thanks in advance,
Hrg
As per my understanding try this
#for (int i = 0; i < Model.PropertyName.Count(); i++)
{
<p>
#Html.CheckBox("model.PropertyName[" + i + "]", !Model.PropertyName[i])
</p>
}
You van use this also
#for (int i = 0; i < Model.PropertyName.Count(); i++)
{
#Html.CheckBoxFor(model => Model.Tags[i])
}
Related
aspx file has 10 labels. Their ID's are like lbl0, lbl1, lbl2...,lbl10.
In the cs file, they will be assigned the values of a list called lst.
lbl0.Text=lst[0];
lbl1.Text=lst[1];
How can I accomplish that in a for loop.
I need sth like this:
for(i=0;i<10;i++)
{
(lbl+i).Text = lst[i];
}
Inside your loop, you can build a string with the label's ID.
string s = String.Format("lbl{0}", i);
And then using something like FindControl() to get the label with that ID.
I think this should work, just use FindControl to get each Label in your loop:
for (int i = 0; i < 10; i++)
{
((Label)(this.FindControl("lbl" + i.ToString()))).Text = lst[i];
}
Have landed into a scenario.
I have a gridview with 22 static columns (few are BoundFields, few are TemplateFields).
We have a scenario in our project that we need to order the GridView according to the columns selected. The order of the columns selected is provided from the UI.
For ex: We have Doc1 to Doc23 as the columns.
Now, from the functionality provided in the UI, I am passing some 4 columns say Doc2,Doc4,Doc5,Doc7.
Now I want that only these 4 columns should be present in my grid as a final output.
Have tried some code, but it doesn't seem to work.
Below is my code:
public int GridColumnOrdering(string columnList)
{
string[] test = columnList.Split(';');
var docCatColumn = gridResultSet.Columns[0];
var docTypeColumn = gridResultSet.Columns[1];
int columnCount = 0;
int testCount = 0;
for (int i = 0; i < test.Count(); i++)
{
if (test[i] == "Doc2")
{
gridResultSet.Columns.Insert(i , docCatColumn);
columnCount++;
}
if (test[i] == "Doc3")
{
gridResultSet.Columns.Insert(i , docTypeColumn);
columnCount++;
}
}
gridResultSet.Columns[2].Visible = false;
gridResultSet.Columns[3].Visible = false;
}
columnList is a parameter passed which has values such as Doc2;Doc3.
My idea is that I get the static columns which resemble the column gotten from the UI, change its position to the very next position, and then hide that static column. In this way, we actually have 2 columns by the same name, but I am trying to hide the static one and display the dynamic one.
I know it sounds weird and hectic, but this is what came to my mind.
Now the problem is that if I try to change the visibility of the static column, the visibility of the dynamic one also changes.
Can experts help on this issue or point out to some easy method in this regards??
Regards
Anurag
We want to set the selection on a XtraGrid based on a List collection from the same type as the datamember of an XtraGrid.
The way we do it now is to iterate the gridview rows.
private void SetSelectedRowsInternal(IList<StrongType> collecshung)
{
grdvSomeGrid.ClearSelection();
grdvSomeGrid.BeginSelection();
for (int i = 0;i < grdvSomeGrid.RowCount;i++)
{
StrongType _strongTyped = ((StrongType)grdvSomeGrid.GetRow(i));
if (collecshung.Where(x => x.Id == _strongTyped.Id).Count() == 1)
grdvSomeGrid.SelectRow(i);
}
grdvSomeGrid.EndSelection();
}
Is there a better way to do this?
It depends on how you fill your grid ... You can directly iterate on the BindingList for instance (using LINQ). But if you find that it is taking more time than needed, then it should be because LINQ functions can be hit more than 1 million time even for a small amount of data.
I recommend you to use a dictionary instead of a LINQed collection.
I came across this pretty little piece of code here:
List<DataTable> result = DTHead.AsEnumerable()
.GroupBy(row => row.Field<int>("MIVID"))
.Select(g => g.CopyToDataTable())
.ToList();
It creates a List of DataTable's based on the value of a particular field.
Is it possible to tweak this code to instead create a List of DataTables based on the number of records? For example, if I have 18 records, I would want that broken into 2 DataTables with 10 rows each (the last two rows of the second table would be empty). If I have 35 records, I end up with 4 DataTable's, the last 5 rows empty on table 4.
Thanks!
This is like a standard paging mechanism:
var result = DTHead.AsEnumerable();
int pageSize = 10;
int pages = result.Count / pageSize + 1;
for (int i = 0; i < pages; i++)
{
var page = result.Skip(i * pageSize).Take(pageSize).Select (t => t.Name)
.CopyToDataTable();
}
Of course within the loop you'd have to do something meaningful with the page variable.
Hi
I am creating online quiz in asp.net c#. For that i have one form that displays testlist in dropdownlist & start button. After clicking 2nd form appears, 2nd form shows one label for question, radiobuttonlist for answers ,next & checkbox for review. I am creating array of random question ids in start button click event of the 1stform. when i click next button in 2nd form then next random question appears, i want array of questions those are checked for review. I used code for arrays of values ( eg.10101) 1 for true & 0 for false as follows but i want array of that question ids those are checked:
int[] a = (int[])Session["values"];//this is array of random question ids created in 1st form
int g;
if (chkmark.Checked == true)
{
g = 1;
}
else
{
g = 0;
}
int[] chkarray = new int[Convert.ToInt32(Session["Counter"]) - 1];
int[] temp1 = (int[])Session["arrofchk"];
int k, no;
if (temp1 == null)
no = 0;
else
no = temp.Length;
for (k = 0; k < no; k++)
{
chkarray[k] = temp1[k];
}
chkarray[j] = g;
Personally, i would use a Dictionary<int, bool> for this.
In the key of the dictionary, you can store the random Question ID, in the value of the pair, you can store the checked item state. It might take you more work now to refactor it, but I believe it will save you a lot of time in the end when you want to do more actions on your quiz items.
Using a dictionary - or at least a well chosen collection, I think it will be easier to get the right data back.
For your example, it can work only if the positions of both arrays are the same.
Dictionary<int, bool> quizAnswers = new Dictionary<int, bool>(); // <questionID, checked>
// Fill dictionary with questions and answers
for(int i=0;i<a.length;i++)
{
if(temp1.length > i) // Make sure we don't get an IndexOutOfBoundsException
{
quizAnswers.Add(a[i], temp1[i] == 1);
}
}
// Get Answered question in array ( LINQ )
int[] checkedAnswers = (from KeyValuePair<int, bool> pair in quizAnswers
where pair.Value == true
select pair.Key).ToArray<int>();
The reason I am using a Dictionary here, is because I personally think it's neater than having two separate arrays.
I believe you should implement a Dictionary in your quiz, in stead of those arrays. What if the array indexes don't match, or you want to dynamically add a question to a fixed size array, etc..
It's something to take into consideration. Hope I could help you out.