Recursivity with panels in ASP.NET - asp.net

I have a database with a table called "categories". The table has 3 columns:
1. ID
2. Name
3. ParentID.
id | Name |ParentID
_____________________
1 | John | 0
2 | Charlie | 1
3 | Vasily | 1
4 | David | 2
5 | Edward | 3
So John is the parent for Charlie and Vasily, David is the child of Charlie and Edward is the child of Vasily.
The question is:
How can i create and populate some panels for each category, considering that the child panels must be inside their parent panels? The question is allmost the same as this one here however i realy realy need it to be with panels instead of treeview.
Thank You.

you should change the code as it is
public class MyObject
{
public int Id;
public int ParentId;
public string Name;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
List<MyObject> list = new List<MyObject>();
list.Add(new MyObject() { Id = 1, Name = "Alice", ParentId = 0 });
list.Add(new MyObject() { Id = 2, Name = "Bob", ParentId = 1 });
list.Add(new MyObject() { Id = 3, Name = "Charlie", ParentId = 1 });
list.Add(new MyObject() { Id = 4, Name = "David", ParentId = 2 });
BindTree(list, null);
}
}
private void BindTree(IEnumerable<MyObject> list, Panel parentPanel)
{
var nodes = list.Where(x => parentPanel == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentPanel.ID));
foreach (var node in nodes) {
Panel newPanel = new Panel() { ID = node.Id.ToString() };
if (parentPanel == null) {
Panel1.Controls.Add(newPanel);
} else {
parentPanel.Controls.Add(newPanel);
}
BindTree(list, newPanel);
}
}

Related

How can set the value of the TableView based from its specific row and column number

I have a Model that accepts a row number, column, and its value. I want to populate a table view where the value of the column is pointing to the value in the Model on the current column. (Not a native English speaker, so sorry).
Here is what I have so far
public class DynamicTableView extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
ObservableList<TableData> observableList= FXCollections.observableArrayList(
new TableData(1, 1, 123.00),
new TableData(1, 2, 124.00),
new TableData(1, 3, 125.00),
new TableData(2, 1, 126.00),
new TableData(2, 2, 127.00),
new TableData(2, 3, 128.00),
new TableData(3, 1, 129.00),
new TableData(3, 2, 130.00),
new TableData(3, 3, 131.19)
);
TableView<TableData> tableView = new TableView<>(observableList);
//Creating columns
TableColumn<TableData, Short> colRow = new TableColumn<>("Row");
TableColumn<TableData, Double> colCol1 = new TableColumn<>("Column 1");
TableColumn<TableData, Double> colCol2 = new TableColumn<>("Column 2");
TableColumn<TableData, Double> colCol3 = new TableColumn<>("Column 3");
//Set the value
colRow.setCellValueFactory(new PropertyValueFactory<>("rowNumber"));
/* colCol1.setCellValueFactory(new PropertyValueFactory<>("value"));
colCol2.setCellValueFactory(new PropertyValueFactory<>("value"));
colCol3.setCellValueFactory(new PropertyValueFactory<>("value")); */
//add columns to table
tableView.getColumns().addAll(colRow, colCol1, colCol2, colCol3);
JFXButton button = new JFXButton("Add Data");
button.setOnAction (e-> {
observableList.add(new TableData(5, 1, 132.20));
});
VBox root = new VBox(10, button, tableView);
root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root, 500, 300));
primaryStage.show();
}
public class TableData {
SimpleIntegerProperty rowNumber;
SimpleIntegerProperty columnNumber;
SimpleDoubleProperty value;
public TableData(int row, int col, double val) {
this.rowNumber = new SimpleIntegerProperty(row);
this.columnNumber = new SimpleIntegerProperty(col);
this.value = new SimpleDoubleProperty(val);
}
}
}
Expected Output
+-----+--------+--------+--------+
| Row | Col 1 | Col 2 | Col 3 |
+-----+--------+--------+--------+
| 1 | 123.00 | 124.00 | 125.00 |
+-----+--------+--------+--------+
| 2 | 126.00 | 127.00 | 128.00 |
+-----+--------+--------+--------+
| 3 | 129.00 | 130.00 | 131.19 |
+-----+--------+--------+--------+
I have seen this example https://stackoverflow.com/a/27497094/14660358, but I could hardly follow and implement it to my problem.
The only solution I have so far was to reconstruct my model to something like this :
public class TableData {
SimpleIntegerProperty rowNumber;
SimpleDoubleProperty column1;
SimpleDoubleProperty column2;
SimpleDoubleProperty column3;
public TableData(int row, double col1, double col2, double col3) {
this.rowNumber = new SimpleIntegerProperty(row);
this.column1 = new SimpleDoubleProperty(col1);
this.column2 = new SimpleDoubleProperty(col2);
this.column3 = new SimpleDoubleProperty(col3);
}
}
Then populate the table's column value property to
colCol1.setCellValueFactory(new PropertyValueFactory<>("column1"));
But I knew this is not a good programming practice. So I was hoping for someone to give atleast a much more detailed example if possible :)
PS: I'm still working on the question's title, so sorry if this misleads someone.

MVC 5, show data by group in Index page

I'm new in MVC5.
I have 2 table, tblClient & tblBranch.
The Data Structure are:
tblClient
ID | Name
---------
1 | BCS
2 | LBBI
3 | CARB
tblBranch
ID | cID | BranchName
---------------------
1 | 1 | Savar
2 | 1 | Nobinogor
3 | 2 | Sawrapara
4 | 1 | Mirpur
5 | 3 | Motijheel
6 | 2 | Dhanmondi
7 | 1 | Kazipara
Now I need to show data in index page as like:
BCS
sL | Branch
-----------
1 | Savar
2 | Nobinogor
3 | Mirpur
4 | Kazipara
LBBI
sL | Branch
-----------
1 | Sawrapara
2 | Dhanmondi
CARB
sL | Branch
-----------
1 | Motijheel
How can I do this ?
Write a model class for your index view.
public class MyModel
{
public string GroupName;
public List<string> GroupItems;
}
Specify your model for your view:
#model IEnumurable<MyProject.MyModel>
#foreach (var item in Model)
{
<h2>#item.GroupName</h2>
<ol>
#foreach (var it in item.GroupItems)
{
<li>#it</li>
}
</ol>
}
And finally in the index action of your controller fill the model and pass it to the view:
public ActionResult Index()
{
var model = new List<MyModel>();
var item = new MyModel();
item.GroupName = "Hello World";
item.GroupItems = new List<string>() { "item1", "item2" };
model.Add(item);
return View(model);
}
You can wrap every tblClient in new Object:
tblClient has name and contains List of tblBranch
public Class Client
{
public string clientName {get;set;}
public List<Branch> branches {get;set;}
}
public Class Branch
{
public string branchName
}
and retriere List<Client> and show them to the UI
There is no need in doing something unusual. Especially, groupping by.
You can simply iterate through items and output it.
In ASP.NET Razor:
Dictionary<int, string> clients = // get dictionary from DB like {{1, "BCS"}, {2, "LBBI"} ...}};
List<BranchEntity> branches = // get list of Branch entities just like in DB;
// You can pass these values as ViewBag or together as a Model
#{
foreach (var client in clients)
{
var branchesForClients = branches.Where(x => x.cID == client.Key).ToArray();
<h3>#client.Value</h3>
<table>
for (int i = 0; i < branchesForClients.Length; i++)
{
<tr>
<td>#(i + 1)</td>
<td>#branchesForClients[i].BranchName</td>
</tr>
}
</table>
}
}
Here is an entity:
public class BranchEntity
{
public int Id { get; set; }
public int cId { get; set; }
public string BranchName { get; set; }
}

How to create a breadcrumb for categories and subcategories using linq

How to create breadcumb for categories with unlimited subcategories which their parent specified with their parentid column?
Imagine the following table entries:
id name parentid
=========================
1 animal NULL
2 veg NULL
3 mineral NULL
4 doggie 1
5 kittie 1
6 horsie 1
7 gerbil 1
8 birdie 1
9 carrot 2
10 tomato 2
11 potato 2
12 celery 2
If a recursive function working on the full set is ok then the following should work.
I threw it together in LINQPad. The magic is the recursive function GetBreadcrumbs. I added a third level "boxer" under doggie.
void Main()
{
var list = new List<MyEntity>()
{
new MyEntity() { Id = 1, Name = "animal" },
new MyEntity() { Id = 2, Name = "veg" },
new MyEntity() { Id = 3, Name = "mineral" },
new MyEntity() { Id = 4, Name = "doggie", ParentId = 1 },
new MyEntity() { Id = 5, Name = "kittie", ParentId = 1 },
new MyEntity() { Id = 6, Name = "horsie", ParentId = 1 },
new MyEntity() { Id = 7, Name = "gerbil", ParentId = 1 },
new MyEntity() { Id = 8, Name = "birdie", ParentId = 1 },
new MyEntity() { Id = 9, Name = "carrot", ParentId = 2 },
new MyEntity() { Id = 10, Name = "tomato", ParentId = 2 },
new MyEntity() { Id = 11, Name = "potato", ParentId = 2 },
new MyEntity() { Id = 12, Name = "celery", ParentId = 2 },
new MyEntity() { Id = 13, Name = "boxer", ParentId = 4 },
};
var breadcrumbs = GetBreadcrumbs(list);
foreach (var breadcrumb in breadcrumbs)
Console.WriteLine(breadcrumb);
}
// This is where the Magic happens!
public IEnumerable<string> GetBreadcrumbs(IEnumerable<MyEntity> entities, int? parentId = null)
{
var parents = entities.Where(x => x.ParentId == parentId);
var children = entities.Where(x => x.ParentId != parentId);
foreach (var parent in parents)
{
yield return parent.Name;
foreach (var trail in GetBreadcrumbs(children, parent.Id))
yield return (parent.Name + " > " + trail);
}
}
public class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
}

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();
}

Bind a list returned from a LINQ query to Gridview

I have a linq query something like this
var Query = from c in table
where (some condition)
select new {
Name = c.Name,
courses = // this returns a list,
};
How do I bind this to gridview so that the result is like this
name1 course1
name1 course2
name1 course3
name2 course1
name2 course2
Any ideas?
try below
gridview.DataSource = Query.ToList().Select(a => a.courses
.Select(c => new { Name = a.Name, Course = c }))
.SelectMany(p=>p).ToList();
gridview.DataBind();
If you want to return this list from method then create class as below
public class MyClass
{
public string Name { get; set; }
public string Course { get; set; }
}
now you can return list as
public List<MyClass> MyMethod()
{
var Query = from c in table
where (some condition)
select new {
Name = c.Name,
courses = // this returns a list,
};
return Query.ToList().Select(a => a.courses
.Select(c => new MyClass{ Name = a.Name, Course = c }))
.SelectMany(p=>p).ToList();
}

Resources