Chee mun Low
Chee mun Low

Reputation: 107

Set GridView BoundField in ASP.Net

I wish to set the datasource as my bound field item, but i failed to do so, my output is not as my expected

output

boundfield userName movieComment

expected output

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

Upvotes: 2

Views: 846

Answers (1)

Amar Palsapure
Amar Palsapure

Reputation: 9680

Try this GridView2.AutoGenerateColumns = false;. Read more on MSDN.

This way you tell GridView not to generate columns on its own.

Another solution can be, change your query to

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

And comment out your custom code for BoundField.

Hope this works for you

Upvotes: 3

Related Questions