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

时间:2011-11-21 06:08:10

标签: c# javascript asp.net gridview

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

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

我试过Gridview1.columns [5] .itemstyle.wrap = false。

但它给我的错误就像

  

错误指数超出范围。必须是非负数且小于集合的大小

请帮帮我。

我的代码是

 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>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

如果您收到错误,例如Index超出范围。必须是非负数且小于集合的大小意味着您将列号设置为多于现有列