I have 6 dropdownlist that are populated from a sqldatasource. I also have another sqldatasource that returns some rows. What I want to do is to go through each row of the second datasource and select that value in my dropdowns. So if second datasource contains three rows it would select the appropriate value in the first three dropdownlist and set the others to "N/A" for example.
我有一个从sqldatasource填充的6个下拉列表。我还有另一个sqldatasource返回一些行。我想要做的是遍历第二个数据源的每一行,并在我的下拉列表中选择该值。因此,如果第二个数据源包含三行,它将在前三个下拉列表中选择适当的值,并将其他数据集设置为“N / A”。
here is some pseudo code I thought of
这是我想到的一些伪代码
protected void fileattributes_ItemDataBound(object sender, ListViewItemEventArgs e)
{
DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1");
DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2");
DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3");
DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4");
DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5");
DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6");
DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
for(int i = 0; i<sqldatasource2.length;i++)
{
array[i].SelectedItem.Text = sqldatasource2.item
}
foreach(Array a in array)
{
if (a is null)
{
a.selecteditem.text = "N/A";
}
}
}
2 个解决方案
#1
1
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();
DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
int i;
for (i = 0; i < dt.Rows.Count;i++ )
{
array[i].SelectedItem.Text = dt.Rows[i][0].ToString();
}
// If array length (number of DropDowns) is greater than rows in datasource
// Remaining Dropdowns will get text N/A --- Now i=dt.Rows.Count
for (; i < array.Length; i++)
{
array[i].SelectedItem.Text = "N/A";
}
#2
1
I'm not very experienced working directly with the sqldatasource, so what I did was to fill a DataTable with the result set. Hope this works!
我不是很有经验直接使用sqldatasource,所以我做的是用结果集填充DataTable。希望这个有效!
DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1");
DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2");
DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3");
DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4");
DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5");
DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6");
DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
DataView dv = new DataView();
DataTable dt = new DataTable();
dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
dt = dv.ToTable();
int i = 0;
foreach (DataRow dr in dt.Rows)
{
// dr["column name or column index"] (zero in my case)
array[i].SelectedItem.Text = dr[0].ToString();
i++;
}
#1
1
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();
DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
int i;
for (i = 0; i < dt.Rows.Count;i++ )
{
array[i].SelectedItem.Text = dt.Rows[i][0].ToString();
}
// If array length (number of DropDowns) is greater than rows in datasource
// Remaining Dropdowns will get text N/A --- Now i=dt.Rows.Count
for (; i < array.Length; i++)
{
array[i].SelectedItem.Text = "N/A";
}
#2
1
I'm not very experienced working directly with the sqldatasource, so what I did was to fill a DataTable with the result set. Hope this works!
我不是很有经验直接使用sqldatasource,所以我做的是用结果集填充DataTable。希望这个有效!
DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1");
DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2");
DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3");
DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4");
DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5");
DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6");
DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
DataView dv = new DataView();
DataTable dt = new DataTable();
dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
dt = dv.ToTable();
int i = 0;
foreach (DataRow dr in dt.Rows)
{
// dr["column name or column index"] (zero in my case)
array[i].SelectedItem.Text = dr[0].ToString();
i++;
}