2011-11-21 18 views
1

我想在c#代码中将gridview列项目样式设置为false。怎么做?如何在c中设置gridview列项目样式#

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

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

,但它给了我像

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

请帮助我。

我的代码是

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

由于错误提示您的列索引超出范围。仅供参考,数组总是基于0的索引。发布您的实施。 –

+0

@SandeepGB,我在databinding()之后应用了上面的代码,但它会给我这样的错误.wt我应该怎么做? –

+0

在调试模式下运行并查看“Gridview1.columns [5]”是否有效(非空)。还要检查“Gridview1.columns” –

回答

1

数据应在提供的数据源存在,并且列数必须大于或等于指数展开列。

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

中声明它们时,我使用它的代码vb.net版本然后它创建像索引超出范围的错误。必须是非负数且小于集合的大小。 – Developer

+0

在您提供的DataSource中应该有数据,如果数据总是可能不可用,则将检查点设置为(ds.Tables.Count> 0){}。另外,如果数据可用,并且如果要将样式放在网格视图的第6列(如给出示例),那么您应该使用GridView1.Columns [5],索引为0,因此请检查给定的列索引,然后存在列号。希望你明白@Singh谢谢。 –

+0

问题没有解决。 – Developer

0

维韦克,

首先,你检查多少列在您的gridview.Once在那里你澄清这一点,那么你检查

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 ; 

如果您收到类似索引的错误超出范围。必须为非负且小于集合的大小意味着你是给列数比现有的列的详细

+0

在这种情况下,我= 1,我有一个列在.aspx页面,列数必须返回确切的列,我已经绑定..吨现在做? –

+0

有问题,你提到你没有列标签.aspx page.You绑定代码后面。现在你告诉你在.aspx页面有一列。可以发布你的代码,这样我就可以以帮助您 – prema

+0

对不起,但我只有一列编辑作为命令字段..我编辑我的Q. –

相关问题