如何使用不同的列名称和与列对应的值填充两个单独的中继器
如何使用两个单独的列表填充转发器
我有以下中继器:
我试图将列名填充到每个lbl2
,并将相应列的行值填充到每个lbl2
。 我有以下代码:
List colO = new List(); List colOV = new List(); List colT = new List(); List colTV = new List(); 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();
您可以以array of muti-dimension
的forms访问您的DataTable
,除了访问行和列时最简单。 例如:
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 } }
每行包含单独的列。 您可以通过Rows [i] .ItemArray访问它