简体   繁体   English

为什么GridView的列数为0

[英]Why column count is 0 for GridView

Friends, I'm populating a GridView in my asp.net application using following code. 朋友,我在asp.net应用程序中使用以下代码填充GridView。

    GridView grdExport = new GridView();
    DataSet dsRecord = objHelper.gReturnDataSet(CommandType.Text, strSql);

    grdExport.DataSource = dsRecord.Tables[0];
    grdExport.DataBind();

Now the problem is in immediate window, when I'm checking, I'm getting following result: 现在问题出在即时窗口中,当我检查时,得到以下结果:

    ?dsRecord.Tables[0].Columns.Count
    16
    ?dsRecord.Tables[0].Rows.Count
    37
    ?grdExport.Rows.Count
    37
    ?grdExport.Columns.Count
    0

Please, can anyone tell me why Column count is 0 for grdExport? 请问谁能告诉我为什么grdExport的列数为0?

It shows the counts = 0 because by default autogenerated columns is true If you add manual colums then it will shows the column counts. 它显示counts = 0因为默认情况下自动生成的列为true如果添加手动列,则它将显示列计数。
If you write grdExport.AutoGenerateColumns = false ; 如果您编写grdExport.AutoGenerateColumns = false ; then no columns would rendered in page. 那么页面中将不会显示任何列。

GridView.Columns Property GridView.Columns属性

Check this: 检查一下:

The Columns property (collection) is used to store all the explicitly declared column fields that get rendered in the GridView control. Columns属性(集合)用于存储在GridView控件中呈现的所有显式声明的列字段。 You can also use the Columns collection to programmatically manage the collection of column fields. 您还可以使用Columns集合以编程方式管理列字段的集合。

If you have more columns to your added columns in your grid then it will show count of those columns which you have added not the auto generated columns. 如果在网格中添加的列中还有更多列,则它将显示已添加的那些列的计数,而不是自动生成的列。

If you show auto generated columns then it will show 0. Check this markup: 如果显示自动生成的列,则它将显示0。检查此标记:

 <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
            </Columns>
        </asp:GridView>

Now it will Show your result of columns's count to 1: 现在它将显示您的列计数结果为1:
//Before adding column to gridview //在将列添加到gridview之前

?dtResult.Rows.Count
9
?dtResult.Columns.Count
2
?GridView1.Rows.Count
9
?GridView1.Columns.Count
0

After Adding column to gridview. 将列添加到gridview之后。

?GridView1.Columns.Count
1

May be it is because you didnt place gridView on page? 可能是因为您没有在页面上放置gridView吗? like this:PlaceHolder1.Controls.Add(grdExport) 像这样:PlaceHolder1.Controls.Add(grdExport)

Instead ?grdExport.Columns.Count. 而是?grdExport.Columns.Count。 This count you get when you add columns collection in gridview at design time. 在设计时在gridview中添加列集合时,您将获得此计数。 You have to use grdExport.Rows[0].Cells.Count 您必须使用grdExport.Rows [0] .Cells.Count

Your GridViewColumn Column will be set after binding your data. 绑定数据后将设置GridViewColumn Column。 So just show a MessageBox.Show to find the column count. 因此,只需显示MessageBox.Show即可找到列数。

grdExport.AutoGenerateColumns = false;
MessageBox.Show(grdExport.Columns.Count.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM