Tree-views in table layout panel and get values dynamically c# - c#

I have multiple treeviews in the table layout panel - C# windows application.
Each cell consists of Treeview or dropdown or textbox. I can get the value of textbox and dropdown but I am unable to get the selected node of multiple Treeviews in the table layout panel.
below my code.
int rows;
int column;
rows = tableLayoutPanel1.RowCount;
column = tableLayoutPanel1.ColumnCount;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
Control value = tableLayoutPanel1.GetControlFromPosition(j, i);
//here i got specified treeview but i can't get refernce. getting error
string controlName = value.Controls.Owner.Name;
//here i got error i am unable to get treeview selected text
string seletedvalue = controlName.SelectedNode.Text;
MessageBox.Show(controlName);
}
}

You are getting the control, but you are not checking what type of control it is, and you are not casting it. So it stays as a general control and not a TreeView. And only the TreeView has nodes.
You can also simply the selection of the specific control that you want.
foreach ( TreeView tv in tableLayoutPanel1.Controls.OfType<TreeView>())
{
string seletedvalue = tv.SelectedNode.Text;
MessageBox.Show(tv.Name + " " + seletedvalue);
}

Related

How to get all Grid view Values in Multi dimensional Array with out selecting or clicking any button?

I do not want to select row or to click button in grid view just to get grid view data on button control that is not in grid view control.
i am trying to get grid view values in array my code is,
GridView GV = GdItemList;
var array = new object[GV.Rows.Count, GV.Columns.Count];
for (int i = 0; i < GdItemList.Rows.Count; i++)
{
foreach (GridViewRow j in GdItemList.Rows)
{
array[j.RowIndex, j.RowIndex] = j.Cells[0];
array[j.RowIndex, j.RowIndex+1] = j.Cells[1];
array[j.RowIndex, j.RowIndex + 2] = j.Cells[2];
//a.Add(gv.Rows[i].Cells[0].Text);
}
}
I am getting error
Index was outside the bound of array on line,
array[j.RowIndex, j.RowIndex] = j.Cells[0];
when i debug it i can find grid view count 1 but here,
foreach (GridViewRow j in GdItemList.Rows)
i do not find any thing and row count is zero here
Hopes for your suggestion
Thanks

loop a gridview and 2 lists to change one cell value, or use some other structure?

I have to programmatically change a value in a GridView cell, depending on whether a compatible value exists or not in a populated List. If the value does exist, then take the value from a second List and modify the GridView cell value.
The logic would be as follows:
Check if the value of the list exists in the cell:
Get the corresponding value from the second list and set it as the Gridview cell value:
The code I was trying to implement is as follows:
if ((originalCombo.Count > 0) && (destinationCombos.Count > 0))
{
for (int k = 0; k < GridView1.Rows.Count; k++)
{
for (int i = 0; i < originalCombo.Count; i++)
{
for (int j = 0; j < destinationCombos.Count; j++)
{
if (GridView1.Rows[k].Cells[6].Text == originalCombo[i].ToString())
{
GridView1.Rows[k].Cells[6].Text = destinationCombos[j].ToString();
}
}
}
}
}
I think I have my entire logic wrong. Maybe I should use another method to iterate between the elements?
EDIT 1: So, probably I should be more clear about what I have and what I want to achieve. The originalCombo and destinationCombos are two lists, which are populated by a certain query. The GridView is already populated at the time I need to check for the value. So, if originalCombo and destinationCombos are not empty, I have to check the last cell on each row of the GridView, and if it's value is equal to one of the items of originalCombo,then I have to replace it with the respective value of destinationCombos

How to select a row in gridview by code based on its key value?

I'm using asp.net web forms application to view data of certain table in grid view & select a row in this grid view based on ID of this data row (Key Value) retrieved from query string
I tried using this code in Code behind
gridview1.SelectedValue= Request.QueryString["RowToSelectID"];
but it said that selected value is a read only property and can't be assigned
Is there another way to do that ?
Try the following and see here for more information.
var keyValue = 1; // Replace with your Convert.ToInt32(Request.QueryString["RowToSelectID"])
for (int i = 0; i <= this.gridview1.DataKeys.Count - 1; i++)
{
if ((int)gridview1.DataKeys[i].Value == keyValue )
{
this.gridview1.SelectedIndex = i;
}
}
I used the SelectedIndex. The record in the GridView having a key value of 1, will be selected.

Highlight a C# ListView row

I'm using a ListView in Details mode to display a list. I want to change the current index in two ways: firstly, by a mouse click (which works now), and secondly with + and - buttons. The problem is that when I click the button, the list loses focus and the row highlight disappears. How do I keep the highlight?
EDIT: Okay, I found the HideSelection property. But how do I change the selected index from the outside?
You can do something simple like this
this.listView1.Items[0].Selected = true;
Or you can iterate throught the list of items and find the one that you want to select.
private void PlusButtonClick()
{
int newIndex = 0;
for (int x = 0; x < listView1.Items.Count; x++)
{
if(listItem.Selected);
{
listItem.Selected = false;
newIndex = x++;
break;
}
}
this.listView1.Items[newIndex].Selected = true;
}

how to disable default selection in dropdownlist in asp.net

i want the code to disable default selection in dropdownlist in asp.net and also on selection of a particular data field the values are displayed in the txtbox below.the dropdown should be filled with system related data like eg c:drive, d ... etc dynamically at run time.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
ok then use this function and cal this function on the databound event of dropdownlist.
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
and call this function in dropdownlist_databound() event.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......

Categories