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:
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
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)
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;
}
Please login first to submit.