【发布时间】: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