如何填充具有不同的列名和对应于列的值两个单独的中继器 [英] How to populate two separate repeaters with distinct column name and the value corresponding to the column
问题描述
如何填充一个中继器有两个单独列表
我有以下中继器:
< DIV的风格=宽度:100%;溢出:隐藏;>
< ASP:直放站ID =rptLabels=服务器的ClientIDMode =静态>
<&HeaderTemplate中GT;
< DIV CLASS =hidOverflow setFloatL smallPadLeft的风格=宽度:45%;浮动:左;>
< / HeaderTemplate中>
<&ItemTemplate中GT;
< DIV CLASS =hidOverflow smallPad>
< DIV CLASS =setFloatL半角vertAlignT>
< ASP:标签ID =LBL1的ClientIDMode =静态=服务器文本=<%#Container.DataItem.ToString()%>>< / ASP:标签> ;
< / DIV>
< DIV CLASS =setFloatL vertAlignT>
< ASP:标签ID =LBL2的ClientIDMode =静态=服务器文本=<%#Container.DataItem.ToString()%>>< / ASP:标签> ;
< / DIV>
< / DIV>
< / ItemTemplate中>
< FooterTemplate>
< / DIV>
< / FooterTemplate>
< / ASP:直放站>
< ASP:直放站ID =rptLabels2=服务器的ClientIDMode =静态>
<&HeaderTemplate中GT;
< DIV CLASS =hidOverflow setFloatL smallPadLeft的风格=宽度:45%;浮动:左;>
< / HeaderTemplate中>
<&ItemTemplate中GT;
< DIV CLASS =hidOverflow smallPad>
< DIV CLASS =setFloatL半角vertAlignT>
< ASP:标签ID =lbl3的ClientIDMode =静态=服务器文本=<%#Container.DataItem.ToString()%>>< / ASP:标签> ;
< / DIV>
< DIV CLASS =setFloatL vertAlignT>
< ASP:标签ID =lbl4的ClientIDMode =静态=服务器文本=<%#Container.DataItem.ToString()%>>< / ASP:标签> ;
< / DIV>
< / DIV>
< / ItemTemplate中>
< FooterTemplate>
< / DIV>
< / FooterTemplate>
< / ASP:直放站>
< / DIV>
我想列名填充到每个 LBL1
和相应列的行值到每个 LBL2
。我有以下代码:
列表<串GT; COLO =新的List<串GT;();
名单,LT;字符串> colOV =新的List<串GT;();
名单,LT;字符串>柯尔特=新的List<串GT;();
名单,LT;字符串> colTV =新的List<串GT;();使用
(康涅狄格州的SqlConnection =新的SqlConnection(gloString))
{
串strQuery = @; //查询生成的数据集
试
{
SqlDataAdapter的大=新SqlDataAdapter的(strQuery,康涅狄格州);
的DataSet myDataSet =新的DataSet();
da.Fill(myDataSet);
的DataTable myDataTable =新的DataTable();
myDataTable = myDataSet.Tables [0];
为(i = 0; I< myDataSet.Tables [0] .Columns.Count / 2;我++)
{
LOP + = myDataSet.Tables [0] .Columns [I] .ColumnName +;
colO.Add(myDataSet.Tables [0] .Columns [I] .ColumnName.ToString()); //列名
colOV.Add(myDataSet.Tables [0] .Rows [0] [I]的ToString()); //列
}
LOP + =\\\
\\\
的行值;
为(INT J =; J< myDataSet.Tables [0] .Columns.Count; J ++)
{
LOP + = myDataSet.Tables [0] .Columns [J]。 .ColumnName +;
colT.Add(myDataSet.Tables [0] .Columns [J] .ColumnName.ToString()); //列名
colTV.Add(myDataSet.Tables [0] .Rows [0] [J]的ToString()); //列
的行值}
rptLabels.DataSource = COLO; //我想popupate美国科罗拉多州变成LBL1和colOV变成LBL2内部rptLabels转发
rptLabels.DataBind();
rptLabels2.DataSource =小马; //我想popupate小马变成lbl3和colTV变成lbl4内部rptLabels2转发
rptLabels2.DataBind();
}
赶上(SQLEXCEPTION)
{
}
}
现在我只能用一个列表的数据源对每一个中继。我怎么可以修改代码来实现以下内容:
rptLabels.DataSource = COLO; //我想popupate美国科罗拉多州变成LBL1和colOV变成LBL2内部rptLabels转发
rptLabels.DataBind();
rptLabels2.DataSource =小马; //我想popupate小马变成lbl3和colTV变成lbl4内部rptLabels2转发
rptLabels2.DataBind();
您数据表
可以在穆蒂维的数组的形式进行访问
,除了是最短暂访问的行和列。例如:
myDt.Rows [0] [0]; //访问第一行和第一列
myDt.Rows [0] [1]; //访问第一行和第二列
myDt.Rows [0] [2]; //访问第一行和第三列
myDt.Rows [1] [0]; //访问第二行和第一列
myDt.Rows [1] [1]; //访问第二行和第二列
myDt.Rows [1] [2]; //获取第二行和第三列
如果你需要可以去所有领域使用两个嵌套退换为
语句:
的for(int i = 0; I< dtData .Rows.Count;我++)//周游行
{
为(INT J = 0; J< dtData.Rows.Count; J ++)//周游列
$ { b $ b VAR valueField = myDt.Rows [I] [J]; //获取当前场
}
}的值
How to populate a repeater with two separate list
I have the following repeater:
<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>
I am trying to populate the column name into each lbl1
and the row value of the corresponding column into each lbl2
. I have the following code:
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)
{
}
}
Right now I can only use one list for the datasource for each repeater. How can I modify the code to achieve the following:
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();
Your DataTable
could to be accessed in the form of array of muti-dimension
, besides being the most briefly to access rows and columns. For example:
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
If you need can go all fields returned using two nested for
statement:
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
}
}
这篇关于如何填充具有不同的列名和对应于列的值两个单独的中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!