Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我无法在页面加载中看到我的Gridview_C#_Asp.net_Gridview - Fatal编程技术网

C# 我无法在页面加载中看到我的Gridview

C# 我无法在页面加载中看到我的Gridview,c#,asp.net,gridview,C#,Asp.net,Gridview,它以前工作过,我可以在网站上看到Gridview。但是现在,我再也看不见了。我不知道我在代码中更改了什么 在SI.aspx页面中,我有: 我想我已经改变了一些代码,这就是为什么gridview不能显示的原因。 以下是SI.aspx.cs背后的代码: private void SetInitialRow() { try { DataTable dt = new DataTable(); DataRow dr = null; dt.

它以前工作过,我可以在网站上看到
Gridview
。但是现在,我再也看不见了。我不知道我在代码中更改了什么

在SI.aspx页面中,我有:


我想我已经改变了一些代码,这就是为什么gridview不能显示的原因。 以下是SI.aspx.cs背后的代码:

private void SetInitialRow()
{
    try
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("CTNS_NO", typeof(string)));
        dt.Columns.Add(new DataColumn("SEAL_NO", typeof(string)));
        dt.Columns.Add(new DataColumn("PCS", typeof(string)));
        dt.Columns.Add(new DataColumn("[W.G.]", typeof(string)));
        dt.Columns.Add(new DataColumn("CBM", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["CTNS_NO"] = string.Empty;
        dr["SEAL_NO"] = string.Empty;
        dr["PCS"] = string.Empty;
        dr["[W.G.]"] = string.Empty;
        dr["CBM"] = string.Empty;

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["SI"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    catch
    {
    }
}

private void AddNewRowToGrid()
{
    try
    {
        int rowIndex = 0;

        if (ViewState["SI"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["SI"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["CTNS_NO"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["SEAL_NO"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["PCS"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["[W.G.]"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["CBM"] = box5.Text;
                    System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["EPSYLOG_DBBConnectionString"].ConnectionString);

                    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = "INSERT into SI ([CTNS_NO],[SEAL_NO],[PCS],[W.G.],[CBM],[name],[bk_nbr],[shipper],[cons],[pol],[pod],[notify],[size]) values('" + box1.Text + "','" + box2.Text + "','" + box3.Text + "','" + box4.Text + "','" + box5.Text + "','" + name.Text + "','" + bn.Text + "','" + shp.Text + "','" + cng.Text + "','" + pol.Text + "','" + POD.Text + "','" + notif.Text + "','" + sz.Text + "')";
                    cmd.Connection = sqlConnection1;

                    sqlConnection1.Open();
                    cmd.ExecuteNonQuery();
                    sqlConnection1.Close();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["SI"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    catch{ }
}

private void SetPreviousData()
{
    try
    {
        int rowIndex = 0;
        if (ViewState["SI"] != null)
        {
            DataTable dt = (DataTable)ViewState["SI"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

                    box1.Text = dt.Rows[i]["CTNS_NO"].ToString();
                    box2.Text = dt.Rows[i]["SEAL_NO"].ToString();
                    box3.Text = dt.Rows[i]["PCS"].ToString();
                    box4.Text = dt.Rows[i]["[W.G.]"].ToString();
                    box5.Text = dt.Rows[i]["CBM"].ToString();

                    rowIndex++;
                }
            }
        }
    }
    catch
    { }

}

protected void Page_Load(object sender, EventArgs e)
{
    Gridview1.DataBind();
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }
    try
    {
        if (Session["MySession"] == "")
        {
            Response.Redirect("authentication.aspx");
        }
        else
        {
            mail.Text = Session["e"].ToString();
        }
    }
    catch
    {
        Response.Redirect("authentication.aspx");
    }
    try
    {
        name.Text = Session["MySession"].ToString();
        bn.Text = Convert.ToString(Request.QueryString["arg1"]);
        shp.Text = Convert.ToString(Request.QueryString["arg2"]);
        cng.Text = Convert.ToString(Request.QueryString["arg3"]);
        pol.Text = Convert.ToString(Request.QueryString["arg4"]);
        POD.Text = Convert.ToString(Request.QueryString["arg5"]);
        notif.Text = Convert.ToString(Request.QueryString["arg6"]);
    }
    catch
    {
        Response.Redirect("authentication.aspx");
    }
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
    AddNewRowToGrid();
}
private void SetInitialRow()
{
尝试
{
DataTable dt=新的DataTable();
数据行dr=null;
添加(新的数据列(“行数”,typeof(字符串));
添加(新数据列(“CTN_编号”,类型(字符串));
添加(新数据列(“密封号”,类型(字符串));
添加(新数据列(“PCS”,类型为(字符串));
添加(新数据列(“[W.G.]”,类型为(字符串));
添加(新数据列(“CBM”,类型为(字符串));
dr=dt.NewRow();
dr[“行数”]=1;
dr[“CTNS_NO”]=string.Empty;
dr[“SEAL_NO”]=string.Empty;
dr[“PCS”]=字符串。空;
dr[“[W.G.]”]=string.Empty;
dr[“CBM”]=字符串。空;
dt.Rows.Add(dr);
//将数据表存储在ViewState中
视图状态[“SI”]=dt;
Gridview1.DataSource=dt;
Gridview1.DataBind();
}
抓住
{
}
}
私有void AddNewRowToGrid()
{
尝试
{
int rowIndex=0;
如果(ViewState[“SI”]!=null)
{
DataTable dtCurrentTable=(DataTable)视图状态[“SI”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{
对于(int i=1;i 0)
{
对于(int i=0;i
这就是Gridview的外观,它是空的,我们应该填充它并将其添加到数据库中:

当您的数据库(或您正在查找的表)不包含任何内容时,会发生此类问题。如果无法向表中添加一些数据,请尝试删除trycatch块,然后您将看到问题出在哪里。

(1)。设置
ShowHeaderWhenEmpty=“True”

ShowHeaderWhenEmpty
是获取或设置一个值,该值指示当列没有数据时GridView控件中列的标题是否可见

<asp:gridview ID="Gridview1" ShowHeaderWhenEmpty="True" ...
    ...
</asp:gridview>

我试过你说的,但仍然没有显示Gridview。我编辑了我的帖子,看看我的Gridview在页面加载中应该是什么样子。。
<asp:gridview ID="Gridview1" ShowHeaderWhenEmpty="True" ...
    ...
</asp:gridview>
protected void Page_Load(object sender, EventArgs e)
{
    //Gridview1.DataBind(); Remove it
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }

    ...

}