英文:
How to set data table two data column as datasource in asp.net dropdown
问题
我有一个名为cachedData的DataTable。
它的值如下:
COL1 | COL2 |
---|---|
12 | - |
- | 59 |
32 | - |
- | 63 |
我想将这些值设置为一个下拉框,而不考虑它们是两列。
例如:下拉框的值应该是12, 59, 32, 63
我尝试了,但这给了我一个错误。请帮助我将这个dataTable的值绑定到下拉框。
if (cachedData != null)
{
this.DigitalChannelSignatureDropDownList.DataTextField = "COL1";
this.DigitalChannelSignatureDropDownList.DataValueField = "COL1";
this.DigitalChannelSignatureDropDownList.DataSource = cachedData;
this.DigitalChannelSignatureDropDownList.DataBind();
}
英文:
I have a DataTable
called cachedData.
It values like this
COL1 | COL2 |
---|---|
12 | - |
- | 59 |
32 | - |
- | 63 |
I want to set these values to a dropdown without considering these as two columns.
Ex : dropdown values should be 12,59,32,63
I tried but this gives me an error.Please help me to bind this dataTable
values to dropdown
if (cachedData != null)
{
this.DigitalChannelSignatureDropDownList.DataTextField = "COL1"+"COL2";
this.DigitalChannelSignatureDropDownList.DataValueField = "COL1"+"COL2";
this.DigitalChannelSignatureDropDownList.DataSource = cachedData;
this.DigitalChannelSignatureDropDownList.DataBind();
}
答案1
得分: 1
以下是您要翻译的内容:
Well, it is not clear what the "-" values are in your table?
However, you could use a union query, and say this:
SELECT COL1 AS MyCol FROM MyTable WHERE COL1 <> ''-''
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 <> ''-''
The output of above then becomes this:
MyCol
-----
12
32
59
63
And perhaps your ''-'' was for null values, so change the above SQL to:
SELECT COL1 AS MyCol FROM MyTable WHERE COL1 is not null
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 is not null
So, now to load up the drop-down list, we have this:
Markup:
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
And code behind is thus this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strSQL =
@"SELECT COL1 AS MyCol FROM MyTable WHERE COL1 <> ''-'
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 <> ''-'";
DropDownList1.DataTextField = "MyCol"
DropDownList1.DataSource = General.MyRst(strSQL);
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,"Please Select"); // optional
}
}
And of course, code to return a datatable from the SQL can be this:
public DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
英文:
Well, it is not clear what the "-" values are in your table?
However, you could use a union query, and say this:
SELECT COL1 AS MyCol FROM MyTable WHERE COL1 <> '-'
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 <> '-'
The output of above then becomes this:
MyCol
-----
12
32
59
63
And perhaps your '-' was for null values, so change the above SQL to:
SELECT COL1 AS MyCol FROM MyTable WHERE COL1 is not null
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 is not null
So, now to load up the drop-down list, we have this:
Markup:
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
And code behind is thus this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strSQL =
@"SELECT COL1 AS MyCol FROM MyTable WHERE COL1 <> '-'
UNION ALL
SELECT COL2 AS MyCol FROM MyTable WHERE COL2 <> '-'";
DropDownList1.DataTextField = "MyCol";
DropDownList1.DataSource = General.MyRst(strSQL);
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,"Please Select"); // optional
}
}
And of course, code to return a datatable from the SQL can be this:
public DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论