【问题标题】:how to set gridview column item style in c#如何在c#中设置gridview列项样式
【发布时间】:2011-11-21 06:08:10
【问题描述】:

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

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 的索引。发布您的实施。
  • @SandeepGB,我在 databinding() 之后应用了上面的代码,虽然它会给我这样的错误 .wt 我应该怎么做?
  • 在调试模式下运行并查看“Gridview1.columns[5]”是否有效(非空)。还要检查“Gridview1.columns”的计数
  • 我可以把这段代码放在哪里。我的意思是在哪个函数(事件)中??

标签: c# javascript asp.net gridview


【解决方案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;
 }

【讨论】:

  • 当我使用它的代码 vb.net 版本时,它会产生错误,例如索引超出范围。必须是非负数且小于集合的大小。
  • 在您提供的 DataSource 中应该有数据,如果数据总是可能不可用,则将检查点设置为 if(ds.Tables.Count>0) { }。此外,如果数据可用并且如果您想将样式放在网格视图的第 6 列(如给定示例),那么您应该使用 GridView1.Columns[5],索引基于 0,因此请检查您给定的列索引是否更大然后是现有的列号。希望你能理解@Singh,谢谢。
  • 我有以下代码。 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 =假 grdvDummyExport.HeaderStyle.BackColor = Drawing.Color.LightSteelBlue grdvDummyExport.RowStyle.Horizo​​ntalAlign = Horizo​​ntalAlign.Center
  • 您只是获取了一个 GridView 的实例,您没有在动态 GridView 中添加任何列,请参阅链接,codedisplay.com/… 您可以了解如何在 GridView 中添加列,添加列后,您可以使用索引或名称访问它们,谢谢
  • 其实我想使用 GridView 从 dt 生成 excel。那么如何在 vb.net 中自定义列宽?我的主要问题是第一列有大文本,所以我想换行?怎么可能?
【解决方案2】:

维韦克,

首先你检查你的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 ;

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

【讨论】:

  • 在这种情况下 i = 1 我在 .aspx 页面中有一个列,列数必须返回我绑定的确切列.. wt 现在要做什么???
  • 在问题中你提到你在 .aspx 页面中没有列标签。你是从后面的代码绑定的。现在你告诉你在 .aspx 页面中有一个列。你可以发布你的代码,以便我能够帮助你
  • 对不起,但我只有一列可作为命令字段进行编辑。我编辑了我的 Q。
  • 您在哪里计算代码中的列数以及您的代码行“Gridview1.columns[5].itemstyle.wrap = false”在哪里
  • 因为错误我删除了这些行......我在绑定后将这些行放在 else 部分......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多