Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have UnBound GridView - C# ASP.Net and I am refreshing this by query.
I have below code -

SqlDataAdapter adp = new SqlDataAdapter("Select Entry_Date, Emp_Name, Qunatity, Comments From Activity_Master", SqlCon);
System.Data.DataTable dt = new System.Data.DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


I want to set Column width = 50 of "Comments" column and want Warp.
C#
GridView1.Columns[3].ItemStyle.Width = 50;
      GridView1.Columns[3].ItemStyle.Wrap = true;


This gives me below error.
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


How do resolve this. Please help.
Posted
Comments
[no name] 4-May-12 3:17am    
Can you provide us the html coding for the same grid.

1 solution

Hi,

Try setting your column width and wrapping style after binding the grid.
use this flow:
C#
if(!IsPostBack){
   SqlDataAdapter adp = new SqlDataAdapter("Select Entry_Date, Emp_Name, Qunatity,             
        Comments From Activity_Master", SqlCon);
   System.Data.DataTable dt = new System.Data.DataTable();
   adp.Fill(dt);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
GridView1.Columns[3].ItemStyle.Width = 50;
GridView1.Columns[3].ItemStyle.Wrap = true;


All the best..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900