2012-02-07 80 views
2

我想设置数据源为我的绑定字段的项目,但我没有这样做,我的输出还不如我预期的设置GridView控件绑定列在ASP.Net

输出

绑定列的userName movieComment

预期输出

绑定列


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(); 

回答

3

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

通过这种方式,您可以告知GridView不要自行生成列。

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

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

,并注释掉您的自定义代码BoundField

希望这对你的作品

+0

它真正的工作,感谢 – 2012-02-07 05:01:07

+0

我只能接受乌拉圭回合在明年8分钟答案,但顺便说我要去了,所以我只会在我接受UR答案回=) – 2012-02-07 05:01:44

+0

没问题...很高兴为你效劳。你可以upvote?从来没有问过任何问题,所以不知道应用的限制。 – 2012-02-07 05:02:51