Visual Studio Repeater:使用多个DataRow

时间:2015-12-18 18:39:13

标签: c# asp.net repeater

如果我只有一个tableRow,那么我的代码就可以了:

 DataRow tableRow = table.NewRow();
 tableRow["DisplayName"] = sqlReader["DisplayName"].ToString();
 table.Rows.Add(tableRow);

如果我添加第二个,我的Repeater什么都不显示,不起作用:

 DataRow tableRow = table.NewRow();
 tableRow["DisplayName"] = sqlReader["DisplayName"].ToString();
 tableRow["ColumnName"] = sqlReader["ColumnName"].ToString();
 table.Rows.Add(tableRow);

添加多个这些的正确代码是什么?

修改

我也试过这个但是没有用:

 DataRow tableRow = table.NewRow();
 tableRow["DisplayName"] = sqlReader["DisplayName"].ToString();
 table.Rows.Add(tableRow);
 DataRow tableRow2 = table.NewRow();
 tableRow2["ColumnName"] = sqlReader["ColumnName"].ToString();
 table.Rows.Add(tableRow2);

这是我的存储过程代码:

SELECT DisplayName AS DisplayName
    FROM FormField
    WHERE EventId = @EventId AND 
    FormId = @FormId AND 
    Visible = 1
    UNION
    SELECT ColumnName AS ColumnName
    FROM FormField
    WHERE EventId = @EventId AND 
    FormId = @FormId AND 
    Visible = 1

编辑:C#

  DataTable table = new DataTable();
            table.Columns.Add("DisplayName", typeof(string));
            table.Columns.Add("ColumnName", typeof(string));

using (SqlConnection sqlConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
                {
                    sqlConn2.Open();

                    using (SqlCommand sqlCmd2 = new SqlCommand())
                    {
                        sqlCmd2.Connection = sqlConn2;
                        sqlCmd2.CommandType = CommandType.StoredProcedure;
                        sqlCmd2.CommandText = "spGetFormFields";
                        sqlCmd2.Parameters.Add("@EventId", SqlDbType.NVarChar).Value = eventId;
                        sqlCmd2.Parameters.Add("@FormId", SqlDbType.NVarChar).Value = formId;
                        sqlCmd2.ExecuteNonQuery();
                        using (SqlDataReader sqlReader = sqlCmd2.ExecuteReader())
                        {
                            while (sqlReader.Read())
                            {
                                DataRow tableRow = table.NewRow();
                                tableRow["DisplayName"] = sqlReader["DisplayName"].ToString();
                                table.Rows.Add(tableRow);
                                DataRow tableRow2 = table.NewRow();
                                tableRow2["ColumnName"] = sqlReader["ColumnName"].ToString();
                                table.Rows.Add(tableRow2);
                            }
                            RepeaterForm.DataSource = table;
                            RepeaterForm.DataBind();
                        }

                        sqlConn2.Close();
                    }
                }

2 个答案:

答案 0 :(得分:3)

像这样的东西。 SqlDataAdapter可以自动填充数据表。如果需要,它也可以进行插入,更新和删除,但不是如图所示:

using (SqlConnection sqlConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString)) 
using (SqlCommand sqlCmd2 = new SqlCommand())
{
    sqlConn2.Open();
    sqlCmd2.Connection = sqlConn2;
    sqlCmd2.CommandType = CommandType.StoredProcedure;
    sqlCmd2.CommandText = "spGetFormFields";
    sqlCmd2.Parameters.Add("@EventId", SqlDbType.NVarChar).Value = eventId;
    sqlCmd2.Parameters.Add("@FormId", SqlDbType.NVarChar).Value = formId;

    using (SqlDataAdapter da = new SqlDataAdapter(sqlCmd2))
    {
        da.Fill(Table);
        RepeaterForm.DataSource = table;
        RepeaterForm.DataBind();
    }

    sqlConn2.Close();
}

答案 1 :(得分:1)

+1给Crowcoders评论上面关于列名全部搞砸了。

如果您确实想这样做,我建议您尝试将查询更改为

SELECT DisplayName AS DisplayName, ColumnName AS ColumnName FROM FormField WHERE EventId = @EventId AND FormId = @FormId AND Visible = 1

看看是否能提供所需的结果。虽然,我仍然会像@Crowcoder建议的那样去做。