DataRepeater - Dynamic data for a ComboBox for each Row

0
0

Hi All

I don’t know if I missed the understanding of the DataRepeater control, so let me explain what I am trying to achieve:

  1. Each Row on the DataRepeater contains a static 4 items – 3 x TextBoxes and 1 x ComboBox.
  2. Each Row on the DataRepeater comes from a database query that can return 0 – 100 items.
  3. Each ComboBox needs to be filled with data from another source and will be different for each Row.
  4. On change of the value of the ComboBox in each Row, I need to Hide/Disable 2 of the text fields and/or change their values

I have had no issue with the first 2 (if I use common values for the ComboBox binding – as per the examples on this site), but I cannot seem to find some code logic that will enable me to do the binding for the ComboBox at each Row level. Lastly, item 4, I need to use the Selected value/index/key for each combo selection to enable/disable/change the Text fields, I may be able to solve this myself if I can get the binding correct, but after making my nearly bald head even balder, I just cannot achieve item 3.

Would appreciate any ideas, since I can easily get proper key/value pairs for each ComboBox this is not the issue, just need to understand how to bind each separately.

Thanks
KLEVA

  • You must to post comments
1
0

Hi Kevin,

For 1,2 and  3 Ideal is to use a class for structure Model like

public class MyData
{
public string Name { get; set; }
public string Lastname { get; set; }

public List<KeyValuePair<string, string>> Checks { get; set; } = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>(“name”,”Name”),
new KeyValuePair<string, string>(“lastname”,”Lastname”),
};

public KeyValuePair<string,string> CurrentCheck { get; set; }

public MyData(int i)
{
CurrentCheck = Checks.First();
if(i % 3 == 0)
{
Checks.Add(new KeyValuePair<string, string>(“middlename”, “Middlename”));
Checks.Add(new KeyValuePair<string, string>(“surname”, “Surname”));
} else if (i % 3 == 1)
{
Checks.Add(new KeyValuePair<string, string>(“surname”, “Surname”));

}
else
{

}
}
}

 

For 4:

DataReapeter have an attribute ItemTemplate : use a UserControl like ItemTemplate and Bind Like this

PS For that sample I use my own Application Logic but is same as you want to do

UserControl : ItemTemplateSupport

Code :

public string CurrentName {
get => textBox1.Text;
set { textBox1.Text = value; }
}
public string Lastname {
get => textBox2.Text;
set { textBox2.Text = value; }
}

public List<KeyValuePair<string,string>> ComboBoxValues
{
set { comboBox1.DataSource = value; }

get => (List<KeyValuePair<string, string>>)comboBox1.DataSource;
}

}

BaseView: DataRepeaterSupport

public partial class DataRepeaterSupport : Page
{
//List Bindable
BindingList<MyData> Data { get; set; }
public DataRepeaterSupport()
{
InitializeComponent();
//Initialization
Data = new BindingList<MyData>();
//Setting DataSource
dataRepeater1.DataSource = Data;

//ItemTemplate created
var itemTemplate = new ItemTemplateSupport();
itemTemplate.Dock = DockStyle.Fill;

//Add Binding on public Property created on ItemTemplateSupport class
itemTemplate.DataBindings.Add(new Binding(“CurrentName”, Data, “Name”));
itemTemplate.DataBindings.Add(new Binding(“Lastname”, Data, “Lastname”));
itemTemplate.DataBindings.Add(new Binding(“ComboBoxValues”, Data, “Checks”));

//Add item template to dataRepeater
dataRepeater1.ItemTemplate.Controls.Add(itemTemplate);
}

}

With UserControl you are free to add logic on your template

 

Best,

Kevin (ITG)

  • You must to post comments
0
0

Code attempts:

{
dataRepeater1.DataSource = Data1; // Defined elsewhere and correct
tbText1.DataBindings.Add(“Text”, Data1, “Text1”);
tbText2.DataBindings.Add(“Text”, Data1, “Text2”);
cboCombo.DisplayMember = “value”;
cboCombo.ValueMember = “key”;
cboCombo.DataSource = GetSelection(dataRepeater1.CurrentItemIndex); // returns a valid key/value pair but only ever returns the first item
}
private List<KeyValuePair<string, string>> GetSelection(int ind) { // Note: Testing only, not actual data
List<KeyValuePair<string, string>> cboList = new List<KeyValuePair<string, string>>();
cboList.Add(new KeyValuePair<string, string>(“~”, “Select…”));
switch (ind) {
case 1:
cboList.Add(new KeyValuePair<string, string>(“ABC”, “This is ABC”));
cboList.Add(new KeyValuePair<string, string>(“RFD”, “This is RFD”));
break;
default:
cboList.Add(new KeyValuePair<string, string>(“NA”, “Not Applicable”));
break;
}
return cboList;
}

  • Kevin Caine
    Hi Kevin Thanks for the ideas, but binding like that won’t really work for my purpose, so I went another route with the FlowLayout instead, this gave me the flexibility I needed and with a decent simple Dictionary to keep track of the selected values for each combo I managed to almost emulate what I was trying to acheive with the DataRepeater. I will keep the DataRepeater and this binding logic for other areas where I need common ComboBoxes as this seems like a simpler solution than adding all the controls manually and keeping seperate track.
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.