如何使用两个单独的列表填充转发器
我有以下转发器:
<div style="width: 100%; overflow: hidden;">
<asp:Repeater ID="rptLabels" runat="server" ClientIDMode="Static">
<HeaderTemplate>
<div class="hidOverflow setFloatL smallPadLeft" style="width: 45%; float: left;">
</HeaderTemplate>
<ItemTemplate>
<div class="hidOverflow smallPad">
<div class="setFloatL halfWidth vertAlignT">
<asp:Label ID="lbl1" ClientIDMode="Static" runat="server" Text="<%# Container.DataItem.ToString() %>"></asp:Label>
</div>
<div class="setFloatL vertAlignT">
<asp:Label ID="lbl2" ClientIDMode="Static" runat="server" Text="<%# Container.DataItem.ToString() %>"></asp:Label>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="rptLabels2" runat="server" ClientIDMode="Static">
<HeaderTemplate>
<div class="hidOverflow setFloatL smallPadLeft" style="width: 45%; float: left;">
</HeaderTemplate>
<ItemTemplate>
<div class="hidOverflow smallPad">
<div class="setFloatL halfWidth vertAlignT">
<asp:Label ID="lbl3" ClientIDMode="Static" runat="server" Text="<%# Container.DataItem.ToString() %>"></asp:Label>
</div>
<div class="setFloatL vertAlignT">
<asp:Label ID="lbl4" ClientIDMode="Static" runat="server" Text="<%# Container.DataItem.ToString() %>"></asp:Label>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
我正在尝试将列名填充到每个lbl1
中,并将相应列的行值填充到每个lbl2
中。我有以下代码:
List<string> colO = new List<string>();
List<string> colOV = new List<string>();
List<string> colT = new List<string>();
List<string> colTV = new List<string>();
using (SqlConnection conn = new SqlConnection(gloString))
{
string strQuery = @""; //query which generates the dataset
try
{
SqlDataAdapter da = new SqlDataAdapter(strQuery, conn);
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
DataTable myDataTable = new DataTable();
myDataTable = myDataSet.Tables[0];
for (i = 0; i < myDataSet.Tables[0].Columns.Count / 2; i++)
{
lop += myDataSet.Tables[0].Columns[i].ColumnName + " ";
colO.Add(myDataSet.Tables[0].Columns[i].ColumnName.ToString()); //column name
colOV.Add(myDataSet.Tables[0].Rows[0][i].ToString()); //row value of the column
}
lop += "\n\n";
for (int j = i; j < myDataSet.Tables[0].Columns.Count; j++)
{
lop += myDataSet.Tables[0].Columns[j].ColumnName + " ";
colT.Add(myDataSet.Tables[0].Columns[j].ColumnName.ToString()); //column name
colTV.Add(myDataSet.Tables[0].Rows[0][j].ToString()); //row value of the column
}
rptLabels.DataSource = colO; //I would like to popupate "colO" into "lbl1" and "colOV" into "lbl2" inside "rptLabels" repeater
rptLabels.DataBind();
rptLabels2.DataSource = colT; //I would like to popupate "colT" into "lbl3" and "colTV" into "lbl4" inside "rptLabels2" repeater
rptLabels2.DataBind();
}
catch (SqlException)
{
}
}
现在我只能为每个转发器使用一个列表作为数据源。如何修改代码以实现以下目的:
rptLabels.DataSource = colO; //I would like to popupate "colO" into "lbl1" and "colOV" into "lbl2" inside "rptLabels" repeater
rptLabels.DataBind();
rptLabels2.DataSource = colT; //I would like to popupate "colT" into "lbl3" and "colTV" into "lbl4" inside "rptLabels2" repeater
rptLabels2.DataBind();
答案 0 :(得分:2)
每行包含单独的列。你可以通过它访问它 行[I] .ItemArray
答案 1 :(得分:2)
您的DataTable
可以array of muti-dimension
的形式访问,除了最简单的访问行和列之外。例如:
myDt.Rows[0][0]; // access the first row and first column
myDt.Rows[0][1]; // access the first row and second column
myDt.Rows[0][2]; // access the first row and third column
myDt.Rows[1][0]; // access the second row and first column
myDt.Rows[1][1]; // access the second row and second column
myDt.Rows[1][2]; // access the second row and third column
如果需要,可以使用两个嵌套的for
语句返回所有字段:
for(int i = 0;i < dtData.Rows.Count;i++)//travels the rows
{
for(int j = 0;j < dtData.Rows.Count;j++)//travels the columns
{
var valueField = myDt.Rows[i][j];//access the value of current field
}
}