How to bind DevExpress Grid to Dictionary - grid

I have a dictionary key value pair
Dictionary<string, string> Details
Details.Add("Name","ABC");
Details.Add("Number","1234");
How can I add this to be a datasource for devexpress grid(header less) so that the grid displays
Col1 Col2
Name ABC
Number 1234
address Some Road

You can use the following approach:
gridControl1.DataSource = new Dictionary<string, string> {
{ "Name", "Abc" },
{ "Number", "1234" },
// ...
};
var gridView = gridControl1.MainView as GridView;
gridView.PopulateColumns();
gridView.Columns["Key"].Caption = "Col1";
gridView.Columns["Value"].Caption = "Col2";

Related

Display result of linq query randomly in listview in Asp.net C#

var getquest = (from q in dc.Exam_Questions
where q.ExamNumber == int.Parse(Examnumber)
select new
{
QuestionTitle = q.QuestionTitle,
correctanswers = q.correctanswers,
ID = q.ID,
});
ListView1.DataSource = getquest;
ListView1.DataBind();
How to display result of linq query randomly in listview in Asp.net C#?
You can create a random data by adding a guid property to your query. After that you order the result by this new property.
var getquest = (from q in dc.Exam_Questions
where q.ExamNumber == int.Parse(Examnumber)
select new
{
RandomId = Guid.NewGuid(),
QuestionTitle = q.QuestionTitle,
correctanswers = q.correctanswers,
ID = q.ID,
});
ListView1.DataSource = getquest.OrderBy(p => p.RandomId).ToList();
ListView1.DataBind();

How do I give my dropdownlist an id?

Hi I tried examples on the site but can't find solution. I get no error but the id is not assigned to my dropdown list. How do I assign an id to this dropdown list?
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }), new { #id = "listId" })
Thanks for any help!
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }))
The first string argument in #Html.DropDownList is its id, remove the id u are giving as attribute.
try below code :-
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }), new { id = "listId" })

Disable or checked previously selected items of checkboxlist?

i want to disable or checkd previously selected items. the selected item were saved in database as userid_checkbox iteams.i need to get selected items by a user. if user id is 10 and selected item is 2 then it will save in database 10_2. so i need to split it to get the second item number and use loop to disable the selected items. i have written as below
int user_id = Convert.ToInt16(Session["user_id"]);
ward w = new ward();
using (DataClassesDataContext db = new DataClassesDataContext())
{
List<string> bednum = (from j in db.wards where w.user_id == user_id select j.wbedno).ToList();
foreach (var bed in bednum)
{
string vals = bed.Split('_')[1];
cbList = (CheckBoxList)pnlControls.FindControl(vals);
// cbList.Items.Add(new ListItem(vals));
var query = from listItem in cbList.Items.Cast<ListItem>()
// join item in bednum on
where listItem.Value == bed
select listItem;
// var query = from l in cbList.Items
foreach (ListItem listItem in query)
listItem.Selected = true;
}
}
Change your code to this:
int user_id = Convert.ToInt16(Session["user_id"]);
CheckBoxList cbList = (CheckBoxList)pnlControls.FindControl("MyCheckBoxListID");
ward w = new ward();//Guessing ward is a class?
using (DataClassesDataContext db = new DataClassesDataContext())
{
List<string> bednum = (from j in db.wards where w.user_id == user_id select j.wbedno).ToList();
foreach (var bed in bednum)
{
string vals = bed.Split('_')[1];
ListItem listItem = cbList.Items.FindByValue(vals);
if (listItem != null) listItem.Selected = true;
}
}

Editable gridview based on list

Is it possible to create a gridview based on a list? I have the following list:
ID = 1
Name = John
Zip = 33141
ID = 2
Name = Tim
Zip = 33139
I want to be able to create an editable gridview with this list
When i bind it to the grid view, it seems to put everyting in one column, and i can't figure out how to get it to seperate it into different columns
Here is my code for setting the DataSource of the GridView:
DataTable table = ConvertListToDataTable(personList);
GridView1.DataSource = table;
GridView1.DataBind();
static DataTable ConvertListToDataTable(List<string> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 7;
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var rd in list)
{
table.Rows.Add(rd);
}
return table;
}
Here is an example:
private class Person
{
int m_iID;
string m_sName;
string m_sZip;
public int ID { get { return m_iID; } }
public string Name { get { return m_sName; } }
public string Zip { get { return m_sZip; } }
public Person(int iID, string sName, string sZip)
{
m_iID = iID;
m_sName = sName;
m_sZip = sZip;
}
}
private List<Person> m_People;
private void ConvertListToDataTable(List<Person> People)
{
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Zip");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
foreach (Person person in People)
{
DataRow row = table.NewRow();
row[col1] = person.ID;
row[col2] = person.Name;
row[col3] = person.Zip;
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
}

Problem while nesting DataTable

I am trying to achieve nesting in DataTable, i.e. a column of DataTable is a DataTable. My code is something like this:
DataTable table = new DataTable();
DataColumn column = new DataColumn("Qualifications", System.Type.GetType("System.Data.DataTable"));
table.Columns.Add(column);
I am getting a runtime error message at line # 2, that says "Column requires a valid DataType". What could be the reason?
I would say that what you are attempting to achieve is not possible using the way you specified. To achieve a relationship between an entity and several sub-entities use a one-to-many relationship between one table and another table.
This means you have two separate tables, call them for instance TableOne and TableMany. In TableOne put all the fields that describe your entity, and make sure to have a primary key. In TableMany put all the fields that describe your sub-entities, and include a field which is of the type of the TableOne primary key field, which relates each sub-entity to its owning entity.
For examle:
TableOne:
int PrimaryKey
nvarchar(50) Name
TableMany:
int ForeignKey
nvarchar(50) Qualification
int Grade
TableOne sample content:
PrimaryKey Name
1 Alice
2 Bob
3 Charlie
TableMany sample content:
ForeignKey Qualification Grade
1 Driving 100
1 Acting 60
1 Singing 30
2 Driving 40
2 Piloting 90
2 Snowboarding 80
3 Dancing 70
3 Tennis 30
Example Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace datatests
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Main();
}
DataSet dataSet;
DataTable TableOne, TableMany;
DataRelation OneToManyRelation;
void Main()
{
dataSet = new DataSet();
TableOne = new DataTable();
var TableOnePK = TableOne.Columns.Add("PrimaryKey", typeof(int));
TableOne.Columns.Add("Name", typeof(string));
TableMany = new DataTable();
var TableManyFK = TableMany.Columns.Add("ForeignKey", typeof(int));
TableMany.Columns.Add("Qualification", typeof(string));
TableMany.Columns.Add("Grade", typeof(int));
dataSet.Tables.Add(TableOne);
dataSet.Tables.Add(TableMany);
TableOne.Constraints.Add("PK", TableOnePK, true);
OneToManyRelation = new DataRelation("OneToMany", TableOnePK, TableManyFK);
TableOne.ChildRelations.Add(OneToManyRelation);
// Populate TableOne with sample data.
AddTableOneRow(1, "Alice");
AddTableOneRow(2, "Bob");
AddTableOneRow(3, "Charlie");
// Populate TableMany with sample data.
AddTableManyRow(1, "Driving", 100);
AddTableManyRow(1, "Acting", 60);
AddTableManyRow(1, "Singing", 30);
AddTableManyRow(2, "Driving", 40);
AddTableManyRow(2, "Piloting", 90);
AddTableManyRow(2, "Snowboarding", 80);
AddTableManyRow(3, "Dancing", 70);
AddTableManyRow(3, "Tennis", 30);
var parentRow=TableOne.Rows[0];
var childRows = parentRow.GetChildRows(OneToManyRelation);
Console.WriteLine("Data for record key #{0}, Name={1}",
parentRow["PrimaryKey"],
parentRow["Name"]);
Console.WriteLine("Qualifications:");
foreach (DataRow childRow in childRows)
{
Console.WriteLine(" {0}: {1}",
childRow["Qualification"],
childRow["Grade"]);
}
}
private void AddTableManyRow(int fk, string qual, int grade)
{
var newRow = TableMany.NewRow();
newRow["ForeignKey"] = fk;
newRow["Qualification"] = qual;
newRow["Grade"] = grade;
TableMany.Rows.Add(newRow);
}
private void AddTableOneRow(int key, string name)
{
var newRow = TableOne.NewRow();
newRow["PrimaryKey"] = key;
newRow["Name"] = name;
TableOne.Rows.Add(newRow);
}
}
}
Sample output:
Data for record key #1, Name=Alice
Qualifications:
Driving: 100
Acting: 60
Singing: 30

Resources