在ASP.Net中设置GridView BoundField

时间:2012-02-07 04:51:02

标签: c# asp.net

我希望将数据源设置为我的绑定字段项,但我没有这样做,我的输出不是我预期的

输出

boundfield userName movieComment

预期输出

的BoundField


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

SqlCommand cmdReadComment = new SqlCommand("SELECT  [userName],[movieComment] FROM [movieCommentTable] WHERE [movieTitle]='" + lblHeadTitle.Text + "'", conn);

SqlDataReader dtrReadComment;
conn.Open();

dtrReadComment = cmdReadComment.ExecuteReader();

GridView2.DataSource = dtrReadComment;

GridView2.RowStyle.Height = 200;


BoundField userNameBF = new BoundField();
userNameBF.DataField = "userName";
userNameBF.ItemStyle.Width = 180;
GridView2.Columns.Add(userNameBF);        

GridView2.DataBind();

1 个答案:

答案 0 :(得分:3)

试试这个GridView2.AutoGenerateColumns = false;Read more on MSDN

这样您就可以告诉GridView不要自己生成列。

另一种解决方案是,将您的查询更改为

SELECT  [userName] FROM [movieCommentTable] WHERE...

并注明BoundField的自定义代码。

希望这适合你