如何在c#中设置gridview列项样式

本文关键字:gridview 样式 设置 | 更新日期: 2023-09-27 18:15:27

我想在c#代码中设置gridview列itemstyle wrap为false。怎么做呢?

我从c#代码中绑定gridview,所以在。aspx页面中,gridview中没有列标签。

i tried gridview .columns[5].itemstyle。

但是它给了我像

这样的错误

ERROR-Index超出范围。必须非负且小于集合

的大小。

请帮帮我。

我的代码是

 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代码

  <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>

如何在c#中设置gridview列项样式

所提供的数据源中必须存在数据,且列数必须大于或等于展开列的索引。

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;
 }

Vivek,

首先检查gridview中有多少列。一旦你明确了这一点,你就可以检查

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 ;

如果你得到一个错误,如索引超出范围。必须非负且小于集合的大小,这意味着您提供的列号大于现有列