我应该在这段代码中修改什么 [英] what should i modify in this code

查看:81
本文介绍了我应该在这段代码中修改什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sql2005开发VS2005.
我在网页上有一个简单的gridview.view从数据库的一个表中加载数据.它显示所有列,如下所示(第1节):

I am working on VS2005 wITH sql2005.
I have a simple gridview on a webpage.which loads the data from one of the tables from the database. It displays all the columns as follows(SECTION 1):

protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("initial catalog=shikhar;integrated security=yes;server=SHAMMI\\SQLEXPRESS");
        da = new SqlDataAdapter("Select id,name,city from rrr", conn);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


现在,我希望以上场景的行为有所不同.我需要使用以下技术(第2节)在datagrid中仅显示两列(而不是三列):


Now, i want the above scenario to behave diferently. What I need is that only two column (instead of three) should be displayed in datagrid using the following technique(SECTION 2) :

<div>
        <asp:GridView ID="GridView1" runat="server" Style="left: 336px; position: relative;top: 164px">
        <Columns>
        <asp:BoundField HeaderText="x" DataField="id" />
    <asp:BoundField HeaderText="y" DataField="id" />
        </Columns>
        </asp:GridView>
    </div>


我的问题是:我应该从第1节中进行哪些修改才能实现此目标(或您可能建议的其他任何内容).因为,当我添加第2节并运行网页时,它向我显示了section1 + section2的所有列.而且我希望它仅显示section2列.


My question is : what should i modify from Section 1 to achieve this.(or anything else that you might suggest). Since, when i added section 2 and run the webpage, it shows me all the section1 + section2 columns. And i want it to show only section2 columns.

推荐答案

您好,
您应该禁用Gridview的自动列生成.
在设计器中修改Gridview属性(AutoGenerateColumns ="False")
例如
Hi,
You should Disable auto column generation of the Gridview.
Either modify Gridview property in designer (AutoGenerateColumns="False")
for Example
<asp:GridView AutoGenerateColumns="False" ...>



或在代码编辑器中



or in the code editor

GridView1.AutoGenerateColumns=False ; //before calling DataBind();
 GridView1.DataBind();







protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("initial catalog=shikhar;integrated security=yes;server=SHAMMI\\SQLEXPRESS");
        da = new SqlDataAdapter("Select id,name,city from rrr", conn);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.AutoGenerateColumns = false ; 
        GridView1.DataBind();
    }


首先,您必须为每列提供唯一的dataField,而在您的代码中则没有.

First you have to give unique dataField for every column, in your code you did not.

<div>
        <asp:GridView ID="GridView1" runat="server" Style="left: 336px; position: relative;top: 164px">
        <Columns>
        <asp:BoundField HeaderText="x" DataField="id" />
    <asp:BoundField HeaderText="y" DataField="name" />
        </Columns>
        </asp:GridView>
    </div>




现在,使选择注释像要绑定到列中的内容一样.

da =新的SqlDataAdapter(从rrr中选择ID,名称",conn);


我想从这里 http://www.msdn.microsoft.com/en/us/datagrid //msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridview.columns.aspx [




Now make select comment like what you want to bind in column.

da = new SqlDataAdapter("Select id,name from rrr", conn);


I would like to go some basic understanding on datagrid from here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx[^]


Hai使用此示例:
Hai Use this Example:
<asp:GridView ID="grdview" runat="server" AutoGenerateColumns="false">
   <Columns>
   <asp:BoundField HeaderText="Name" DataField="name" />
   <asp:BoundField HeaderText="Address" DataField="address" />
   </Columns>
   </asp:GridView>


这篇关于我应该在这段代码中修改什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆