I can have a treeview that's populated from database and have a checkbox for each item....however, I want to design it so that each node/item has 3 or possibly 4 checkboxes horizontally.....Is there a good design for this...how do I attach a checkboxlist
or individual mutliple checkboxes per item?
There is property called ShowCheckBoxes in treecontrol, it is used to gets or sets a value indicating which node types will display a check box in the TreeView control.
You can set ShowCheckBoxes="All".
The code:
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
</asp:TreeView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = this.GetData("SELECT Id, Name FROM Test38");
PopulateTreeView(dt, 0, null);
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["Name"].ToString(),
Value = row["Id"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT Id, Name FROM Test39 WHERE CustomerId = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
The result:
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
I can do this...but what I need is per each of the treenodes and it's childnodes, I need to have a horizontal checkboxlist with 3 checkboxes in each.....
so by Sam 1, I need to have three checkboxes and same for n1, n2, n3, Sam2 and the rest of it. The levels could be as deep as 5 or more levels as it's all dynamic. I need to be able to have three columns per each of the nodes and have
a title at the top for each of the checkboxes.
I can do this...but what I need is per each of the treenodes and it's childnodes, I need to have a horizontal checkboxlist with 3 checkboxes in each.....
so by Sam 1, I need to have three checkboxes and same for n1, n2, n3, Sam2 and the rest of it. The levels could be as deep as 5 or more levels as it's all dynamic. I need to be able to have three columns per each of the nodes and have
a title at the top for each of the checkboxes
According to your description, I couldn’t understand your requirement clearly.
Do you mean you want to create horizontal nodes?
But as far as I know, the standard TreeView control doesn't provide the horizontal orientation.
Unless you could write a custom control or you could try using a horizontal
Menu control, if it will fit your requirements.
If I misunderstand your requirement, please post more details information about your requirement(You can post a style image).
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
"According to your description, I couldn’t understand your requirement clearly."
I believe the requirement from inkaln is clear: 3 checkboxes beside each node, not just one. (There is nothing in the TreeView control that supports that requirement out of the box.)
Member
95 Points
431 Posts
dynamic treeview control with multiple checkboxes per node/item
Aug 19, 2019 08:19 PM|inkaln|LINK
I can have a treeview that's populated from database and have a checkbox for each item....however, I want to design it so that each node/item has 3 or possibly 4 checkboxes horizontally.....Is there a good design for this...how do I attach a checkboxlist or individual mutliple checkboxes per item?
Contributor
3370 Points
1409 Posts
Re: dynamic treeview control with multiple checkboxes per node/item
Aug 20, 2019 03:08 AM|samwu|LINK
Hi inkaln,
There is property called ShowCheckBoxes in treecontrol, it is used to gets or sets a value indicating which node types will display a check box in the TreeView control.
You can set ShowCheckBoxes="All".
The code:
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All"> </asp:TreeView> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = this.GetData("SELECT Id, Name FROM Test38"); PopulateTreeView(dt, 0, null); } } private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) { foreach (DataRow row in dtParent.Rows) { TreeNode child = new TreeNode { Text = row["Name"].ToString(), Value = row["Id"].ToString() }; if (parentId == 0) { TreeView1.Nodes.Add(child); DataTable dtChild = this.GetData("SELECT Id, Name FROM Test39 WHERE CustomerId = " + child.Value); PopulateTreeView(dtChild, int.Parse(child.Value), child); } else { treeNode.ChildNodes.Add(child); } } } private DataTable GetData(string query) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } }
The result:
Best regards,
Sam
Member
95 Points
431 Posts
Re: dynamic treeview control with multiple checkboxes per node/item
Aug 20, 2019 03:44 PM|inkaln|LINK
I can do this...but what I need is per each of the treenodes and it's childnodes, I need to have a horizontal checkboxlist with 3 checkboxes in each.....
so by Sam 1, I need to have three checkboxes and same for n1, n2, n3, Sam2 and the rest of it. The levels could be as deep as 5 or more levels as it's all dynamic. I need to be able to have three columns per each of the nodes and have a title at the top for each of the checkboxes.
he result:
Contributor
3370 Points
1409 Posts
Re: dynamic treeview control with multiple checkboxes per node/item
Aug 21, 2019 07:38 AM|samwu|LINK
Hi inkaln,
According to your description, I couldn’t understand your requirement clearly.
Do you mean you want to create horizontal nodes?
But as far as I know, the standard TreeView control doesn't provide the horizontal orientation.
Unless you could write a custom control or you could try using a horizontal Menu control, if it will fit your requirements.
If I misunderstand your requirement, please post more details information about your requirement(You can post a style image).
Best regards,
Sam
Contributor
6051 Points
2515 Posts
Re: dynamic treeview control with multiple checkboxes per node/item
Aug 22, 2019 03:18 PM|KathyW|LINK
"According to your description, I couldn’t understand your requirement clearly."
I believe the requirement from inkaln is clear: 3 checkboxes beside each node, not just one. (There is nothing in the TreeView control that supports that requirement out of the box.)