我有2个包含文本框和组合框的列表。 这里创建了文本框和组合框:
System.Windows.Controls.TextBox newTxt = new TextBox();
System.Windows.Controls.ComboBox newcombo = new ComboBox();
// Add Textbox
newTxt.Text = col.Field<String>("ColumnName");
newTxt.Name = col.Field<String>("ColumnName");
newTxt.Width = 110;
// Add Combobox
newcombo.Items.Add(myReader.GetDataTypeName(n));
newcombo.Items.Add("INT");
newcombo.SelectedItem = myReader.GetDataTypeName(n);
newcombo.Name = myReader.GetDataTypeName(n);
newcombo.Width = 90;
在这里我创建了两个列表:
List<TextBox> textboxes = sp.Children.OfType<TextBox>().ToList();
List<ComboBox> comboboxes = sp.Children.OfType<ComboBox>().ToList();
现在我想要一个MySQL Query来编辑一个列。所以我需要ColumnName(textbox.Name)和DataType(combobox.SelectedItem) 我需要一个foreach:
foreach (var count in textboxes)
{
var selected = "i dont know";
config conf = new config();
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " + count.Name + " " + count.Text + " " + selected + " NOT NULL";
MessageBox.Show(cmd.CommandText);
}
对于文本框,我可以使用count.Name获取名称,使用count.Text获取文本框的文本。但是我如何在这个foreach中从组合框中获得.SelectedItem?
答案 0 :(得分:1)
如果你有相同数量的文本框和组合框,你可以使用索引遍历列表,如下所示:
for (int i=0; i< textboxes.Count; i++)
{
//with i you can access specific textbox from one list and combo from another
var tb = textboxes[i];
var cb = comboboxes[i];
}
答案 1 :(得分:0)
您可以尝试使用UserControl类型的一个列表。 Sinds Textbox和Combobox继承自UserControl。
您使用的唯一属性是:名称和文本这些属性来自基本UserControl。
// Add Textbox
TextBox newTxt = new TextBox();
newTxt.Text = col.Field<String>("ColumnName");
newTxt.Name = col.Field<String>("ColumnName");
newTxt.Width = 110;
// Add Combobox
ComboBox newcombo = new ComboBox();
newcombo.Items.Add(myReader.GetDataTypeName(n));
newcombo.Items.Add("INT");
newcombo.SelectedItem = myReader.GetDataTypeName(n);
newcombo.Name = myReader.GetDataTypeName(n);
newcombo.Width = 90;
controls.Add(newcombo);
controls.Add(newTxt);
var controls = sp.Children.OfType<UserControl>().ToList();
foreach (var control in controls )
{
var selected = "i dont know";
config conf = new config();
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN " + control.Name + " " + control .Text + " " + selected + " NOT NULL";
MessageBox.Show(cmd.CommandText);
}
答案 2 :(得分:0)
由于您与TextBoxes
和ComboBoxes
的关系为1:1,因此您应该在代码中反映这种关系,以确保获得您期望的值。如果您知道文本框的值是唯一的,则使用类似Dictionary
的内容:
Dictionary<string, object> d = new Dictionary<string, string>();
其中Value
应替换为combobox
值类型,而textbox
字符串应为Key
,那么您的循环就是:
foreach (var de in d)
{
var selected = "i dont know";
config conf = new config();
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN "
+ d.Key + " " + d.Value + " " + selected + " NOT NULL";
MessageBox.Show(cmd.CommandText);
}
或private
嵌套class
用于处理关系:
private class Strings
{
public string A {get;set;}
public string B {get;set;}
public Strings(TextBox a, ComboBox b)
{
this.A = a.Name; this.B = b.SelectedValue.ToString();
}
}
然后像这样设置:
List<Strings> x = new List<strings>();
x.add(new Strings(textboxcontrol, combocontrol))
然后通过以下方式检索:
foreach (var o in x)
{
var selected = "i dont know";
config conf = new config();
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "ALTER TABLE firmenkunden CHANGE COLUMN "
+ o.A + " " + o.B + " " + selected + " NOT NULL";
MessageBox.Show(cmd.CommandText);
}
我会推荐更好的变量和类名,我只是快速回复:)