2

i want to set gridview column itemstyle wrap to false in c# code. how to do that?

iam binding gridview from c# code so in .aspx page there are no columns tag in gridview.

i tried Gridview1.columns[5].itemstyle.wrap = false.

but it gives me error like

ERROR-Index was out of range. Must be non-negative and less than the size of the collection

please help me.

my code is

 GridView1.DataKeyNames = new string[] { "MemberId" };
        if (Page.IsPostBack)
        {
            if (TextBox1.Text != "")
            {
                SqlConnection con = new SqlConnection(str);
                con.Open();

                string search = TextBox1.Text + "%";


                string query = "Select DISTINCT Designation +' '+FName+ ' ' +MName+ ' ' +LName as Name,"
             + " HomePhone,MobileNo1,"
             + " UPPER(ResAddr1) ++'  '+ UPPER(Resaddr2) ++'  '+ UPPER(ResAddr3) ++'  '+ UPPER(Resaddr4) ++'  '+ UPPER(Resaddr5) ++'  '+ UPPER(Resaddr6) ++'  '+ Pincode ++'  '+City as Address,"
             + " g.Category,f.GroupName as 'Group',Seats,"
             + " dbo.CONCATWTOTSHOW(d.MemberId,d.GID,d.CID)As SeatNo,"
             + " AmountExpected,AmountReceived,Discount,AmountPending,b.Remarks as Reference,b.SplRemarks, (d.MemberId)"
             + " from Person_Master a INNER JOIN Member_Master b ON a.PersonId=b.PersonId"
             + " LEFT JOIN Payment_Master c ON b.MemberId = c.MemberId"
             + " INNER JOIN SeatAssign_Master d ON b.MemberId = d.MemberId"
             + " INNER JOIN Year_Master e ON b.Year = e.Id"
             + " INNER JOIN Group_Master f ON d.Gid=f.Gid"
             + " INNER JOIN Category_Master g ON d.Cid=g.Cid "
             + " where b.Year=" + DDLYear.SelectedValue.ToString() + " and FName + ' ' + LName like '" + search + "' and b.Active=1 and d.Active=1 ";

                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    Label1.Text = "No Records Found !!!";
                    TextBox1.Text = "";

                }
                else
                {
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    Label1.Text = "";
                    TextBox1.Text = "";

                }
            }

        }

.aspx code is

  <asp:GridView ID="GridView1"  runat="server" style="width:120%;font-size:12px;font-family:Tahoma;" 
         BackColor="White" BorderColor="#CC9966" BorderStyle="None" 
         BorderWidth="1px" CellPadding="4" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" 
            AutoGenerateDeleteButton="True" onrowdeleting="GridView1_RowDeleting" 
            onrowcreated="GridView1_RowCreated" >    
            <Columns>
            <asp:CommandField ShowSelectButton="True" SelectText="Edit" />

            </Columns>

        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <SortedAscendingCellStyle BackColor="#FEFCEB" />
        <SortedAscendingHeaderStyle BackColor="#AF0101" />
        <SortedDescendingCellStyle BackColor="#F6F0C0" />
        <SortedDescendingHeaderStyle BackColor="#7E0000" />
    </asp:GridView>
4
  • As the error is saying your column index is out of range. FYI, array is always 0 based index. Post your implementation. Commented Nov 21, 2011 at 6:15
  • @SandeepGB, i applied above code after databinding() though it will give me error like this .wt should i do? Commented Nov 21, 2011 at 6:18
  • run in debug mode and see if "Gridview1.columns[5]" is valid (non-null). Also check the count of "Gridview1.columns" Commented Nov 21, 2011 at 6:20
  • where i can put this code. i mean in which function(event) ?? Commented Nov 21, 2011 at 6:34

2 Answers 2

2

Data should exists in supplied DataSource and the number of columns must be greater or equal to the index of unwrapping column.

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
     int numberOfColumn = ds.Tables[0].Columns.Count;
     if (numberOfColumn >= 5) // Since the 5th column you want to unwrap
     GridView1.Columns[5].ItemStyle.Wrap = false;
 }
Sign up to request clarification or add additional context in comments.

5 Comments

when i used it code vb.net version then it create error like as Index was out of range. Must be non-negative and less than the size of the collection.
In your supplied DataSource should have data, if data always may not be available then put a check point as if( ds.Tables.Count>0) { }. Also, if data is available and if you want to put style at 6th column (as given example) of your grid view then you should use as GridView1.Columns[5], Index is 0 based so check your given column index which is greater then existing column number. Hope you understand @Singh Thanks.
i have following code. Dim grdvDummyExport As New GridView() grdvDummyExport.AllowPaging = False grdvDummyExport.DataSource = ds.Tables(0) grdvDummyExport.DataBind() grdvDummyExport.Columns(0).ItemStyle.Width = 100 grdvDummyExport.Columns(0).ItemStyle.Wrap = False grdvDummyExport.HeaderStyle.BackColor = Drawing.Color.LightSteelBlue grdvDummyExport.RowStyle.HorizontalAlign = HorizontalAlign.Center
You just have taken an instance of GridView only, you didn't add any column in your dynamic GridView, please see the link, codedisplay.com/… you can get idea about how to add column in a GridView, after adding column you can access them with index or name, Thanks
Actually i want to generate excel from dt using GridView. So how can i customize column width in vb.net ? My main Problems are first column have large text so i want to make wrap text ? How can it possible ?
1

Vivek,

First you check how many columns are there in your gridview.Once you clarify that, then you check

GridView1.datasource = ds.tables[0];
Gridview1.databind();
//this will give you how many columns are there in GridView.
int i = GridView1.Columns.Count;
Gridview1.columns[5].itemstyle.wrap = false ;

If you are getting an error like Index was out of range. Must be non-negative and less than the size of the collection means that you are giving the column number as more than the existing columns

11 Comments

in this case i = 1 which i have a column in .aspx page,column count must return the exact columns which i have bound.. wt to do now???
In question you mentioned that you don't have column tags in .aspx page.You are binding from code behind.Now you are telling you have a column in .aspx page.Can u pls post your code so that i will be able to help you
sorry , but i have only one column for edit as command field.. i edited my Q.
where are you counting the number of columns in your code man and where is your code line "Gridview1.columns[5].itemstyle.wrap = false"
i deleted those lines because of an error ... i put those lines in else part after binding....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.