C# GridView没有';不显示

C# GridView没有';不显示,c#,C#,在这里开发一个基本的C#web应用程序,而我似乎无法让GridView显示出来。我可以看到它正在传递代码,因为它将我的图像从一个更改为另一个,但由于某些原因,Gridview没有显示。使用回发按钮和我的下拉框可以获得存储过程的变量。我是C#web应用的新手(这里是Vb Win app guy),所以我需要一些指导 using System; using System.Collections.Generic; using System.Linq; using System.Web; using S

在这里开发一个基本的C#web应用程序,而我似乎无法让GridView显示出来。我可以看到它正在传递代码,因为它将我的图像从一个更改为另一个,但由于某些原因,Gridview没有显示。使用回发按钮和我的下拉框可以获得存储过程的变量。我是C#web应用的新手(这里是Vb Win app guy),所以我需要一些指导

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        //This is where my postback begins...
        if (IsPostBack)
        {
            string Quote = null;

            System.Data.SqlClient.SqlConnection myDatabaseConnection = null;
            System.Data.SqlClient.SqlCommand myCommand = null;
            System.Data.SqlClient.SqlDataReader myReader = null;

            // Validate the SP
            Page.Validate();
            if (Page.IsValid)
            {
                Quote = DropDownList1.Text.ToString();

                try
                {
                    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Data1"].ConnectionString;
                    myDatabaseConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                    myCommand = new System.Data.SqlClient.SqlCommand();

                    //Set up to use my stored procedure:
                    myCommand.CommandType = System.Data.CommandType.StoredProcedure;
                    myCommand.Connection = myDatabaseConnection;
                    myCommand.CommandText = "EditDataPage";
                    myCommand.Parameters.AddWithValue("@QuoteNumber", Quote);


                    //Use an SqlDataReader to execute the stored procedure and
                    //get the results into the GridView:
                    myDatabaseConnection.Open();
                    myReader = myCommand.ExecuteReader();
                    GridView1.DataSource = myReader;
                    GridView1.DataBind();
                    myDatabaseConnection.Close();
                    myDatabaseConnection.Dispose();
                    GridView1.Visible = true;
                    Image1.Visible = false;
                    Image2.Visible = true;
                }
                catch (System.Data.SqlClient.SqlException exception)
                {
                    ErrorLabel.Visible = true;
                }
                catch (Exception exception)
                {
                    ErrorLabel.Visible = true;
                    //Do cleanup tasks here:
                }
                finally
                {
                    myCommand = null;

                    if ((myReader != null) && !myReader.IsClosed)
                    {
                        myReader.Close();
                    }

                    myReader = null;

                    if ((myDatabaseConnection != null) && myDatabaseConnection.State == System.Data.ConnectionState.Open)
                    {
                        myDatabaseConnection.Close();
                        myDatabaseConnection.Dispose();
                    }

                    myDatabaseConnection = null;
                }
            }
        }
    }

}

这是我的aspx代码:

编辑数据

使用控件在数据集中移动,然后编辑并保存所需内容 谢谢你的报告。


我不是100%确定,但您的GridView上有
AutoGenerateColumns=“false”
,但没有定义任何列,这一事实看起来有点可疑。我想作为一个快速测试,您可以将此属性更改为
true
,然后看看会发生什么

如果您确实希望自己指定列(在我的经验中,这是正常的),那么您需要指定它们,例如:

<asp:GridView ...>
   <Columns>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:TemplateField>
         <asp:Label Text='<%# Eval("Surname") %>' runat="server" />
      </asp:TemplateField>
   </Columns>
</asp:GridView>

了解更多详细信息。

带有gridview的aspx页面部分会有所帮助。您是否使用UpdatePanels?你能在aspx页面上发布一些代码吗?您确定您的过程实际上返回了任何行吗?GridView上是否有空的数据模板(可能不是确切的名称)?在这里放一条小消息会让你知道是否没有返回任何结果。好的,我添加了一些我的aspx代码人员,谢谢你的帮助!在您发布的标记中似乎找不到您的GridView。非常感谢,是的,这似乎就是问题所在!我把它改成了真的,现在它可以工作了!
<asp:GridView ...>
   <Columns>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:TemplateField>
         <asp:Label Text='<%# Eval("Surname") %>' runat="server" />
      </asp:TemplateField>
   </Columns>
</asp:GridView>