gridview没有显示什么是解决方案 [英] gridview not displayed what's the solution

查看:91
本文介绍了gridview没有显示什么是解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下......

 <   asp:标签    ID   =   lbl_selcat    runat  < span class =code-keyword> =  server   文本  = 选择目录: >  
< asp:DropDownList ID = ddl_catalogue runat = 服务器 >

< asp:GridView ID = GridView1 < span class =code-attribute> runat = server DataKeyNames = id < span class =code-attribute> onrowcancelingedit = GridView1_RowCancelingEdit

onrowdeleting = GridView1_RowDeleting onrowediting = < span class =code-keyword> GridView1_RowEditing

onrowupdating = GridView1_RowUpdating

AutoGenerateColumns = False BorderColor = 黑色 BorderWidth = 2px

< span class =code-attribute> HeaderStyle-BackColor = #FF6600 < span class =code-attribute> HeaderStyle-BorderStyle = NotSet

HeaderStyle-Font-Bold = True HeaderStyle-Font-斜体 =

< span class =code-attribute> HeaderStyle-Font-Size = HeaderStyle-Font-Underline =

< span class =code-attribute> HeaderStyle-ForeColor = 白色 >
< >

< asp:BoundField DataField < span class =code-keyword> = id HeaderText = ID / >
< asp:BoundField DataField = < span class =code-keyword> cat_id HeaderText = cat_id / >
< asp:BoundField HeaderText = 名称 DataField = name / >
< asp:BoundField DataField = type HeaderText = 输入 / >
< asp:BoundField < span class =code-attribute> DataField = price HeaderText = 价格 / >
< asp:ImageField DataImageUrlField = image HeaderText = 图片 >
< control style < span class =code-attribute> height = 200px width = 200px / >

< span class =code-keyword>< asp:BoundField DataField = description HeaderText = 描述 / >
< asp:CommandField ShowEditButton = True / >
< asp:CommandField ShowDeleteButton = True / >
< /列 >

< HeaderStyle BackColor = #FF6600 Font-Bold = Tru e Font-Italic = 字体大小 = 字体下划线 = True ForeColor = 白色 > < / HeaderStyle >







aspx.cs文件



  protected  空隙 Page_Load( object  sender,EventArgs e)
{
if (IsPostBack! = true
{
ddl_catalogue.DataSource = GetData();
ddl_catalogue.DataValueField = cat_id;
ddl_catalogue.DataTextField = cat_nm;
ddl_catalogue.DataBind();
ddl_catalogue.Items.Insert( 0 new ListItem( ---选择--- 0\" ));


}

}

受保护 void ddl_catalogue_SelectedIndexChanged( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection( @ Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \Database.mdf; Integrated Security = True; User Instance = True);
SqlCommand cmd = new SqlCommand( select *来自cat_detail,其中cat_id =' + ddl_catalogue.SelectedValue + ',con) ;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();

}
dr.Close();
con.Close();
}
DataTable GetData()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection( @ 数据源= .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \Database.mdf; Integrated Security = True; User Instance = True);
SqlCommand cmd = new SqlCommand( select *来自目录,con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
return dt;
}

解决方案

绑定时使用 DataAdapter GridView

 SqlDataAdapter dataAdapter =  new  SqlDataAdapter (cmd,con); 
DataTable dt = new DataTable();
dataAdapter.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();



在初始化 SqlCommand 对象时也使用参数化查询。


my code as below...

<asp:Label ID="lbl_selcat" runat="server" Text="Select catalogue:">
       <asp:DropDownList ID="ddl_catalogue" runat="server">
       
      <asp:GridView ID="GridView1" runat="server"  DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit" 

          onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 

          onrowupdating="GridView1_RowUpdating"

       AutoGenerateColumns="False" BorderColor="Black" BorderWidth="2px" 

          HeaderStyle-BackColor="#FF6600" HeaderStyle-BorderStyle="NotSet" 

          HeaderStyle-Font-Bold="True" HeaderStyle-Font-Italic="True" 

          HeaderStyle-Font-Size="Medium" HeaderStyle-Font-Underline="True" 

          HeaderStyle-ForeColor="White">
           <columns>
           
             <asp:BoundField DataField="id" HeaderText="ID" />
               <asp:BoundField DataField="cat_id" HeaderText="cat_id" />
               <asp:BoundField HeaderText="Name" DataField="name" />
               <asp:BoundField DataField="type" HeaderText="Type" />
               <asp:BoundField DataField="price" HeaderText="Price" />
               <asp:ImageField DataImageUrlField="image" HeaderText="IMAGE">
               <controlstyle height="200px" width="200px" />
               
               <asp:BoundField DataField="description" HeaderText="Description" />
               <asp:CommandField ShowEditButton="True" />
               <asp:CommandField ShowDeleteButton="True" />
              </columns>

<HeaderStyle BackColor="#FF6600" Font-Bold="True" Font-Italic="True" Font-Size="Medium" Font-Underline="True" ForeColor="White"></HeaderStyle>




aspx.cs file

protected void Page_Load(object sender, EventArgs e)
   {
       if (IsPostBack != true)
       {
           ddl_catalogue.DataSource = GetData();
           ddl_catalogue.DataValueField="cat_id";
           ddl_catalogue.DataTextField = "cat_nm";
           ddl_catalogue.DataBind();
           ddl_catalogue.Items.Insert(0, new ListItem("---Select---", "0"));


       }

   }

 protected void ddl_catalogue_SelectedIndexChanged(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
       SqlCommand cmd = new SqlCommand("select * from cat_detail where cat_id='"+ddl_catalogue.SelectedValue+"'", con);
       con.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.HasRows)
       {
         GridView1.DataSource = dr;
         GridView1.DataBind();

       }
               dr.Close();
               con.Close();
   }
   DataTable GetData()
   {
           DataTable dt=new DataTable();
           SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
           SqlCommand cmd=new SqlCommand("select * from catalogue",con);
           con.Open();
           SqlDataAdapter sda=new SqlDataAdapter(cmd);
           sda.Fill(dt);
           return dt;
   }

解决方案

Use DataAdapter while binding GridView.

SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd, con);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();


Also use Parameterized query while initializing a SqlCommand object.


这篇关于gridview没有显示什么是解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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