簡體   English   中英

添加新列時無法獲取GRIDVIEW

[英]Unable to get GRIDVIEW when new column added

這是我的帶有gridview的SEARCH函數。我無法在Form中獲得gridview,其余一切都很好。在這里我添加了新的第二列。請幫幫我。謝謝

private void txtSearch02()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(myStr);
        SqlCommand cmd = new SqlCommand("select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode='" + txtkey2.Text.Trim() + "') or (ItemName is null or ItemName='" + txtkey2.Text.Trim() + "')", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        Session["ItemCode"] = dt;
        con.Open();
        DataSet ds = new DataSet();
        sda.Fill(ds);
        dt = ds.Tables[0];
        con.Close();
        dt.Columns.Add("Quantity");
        dt.Columns.Add("TotalPoints");

        // for caliculation 
        txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();
        //dt.Rows[0]["Quantity"].ToString();
        //dt.Rows[0]["TotalPoints"].ToString();
        DataRow dr;
        dr = dt.NewRow();
        dt.Rows.Add(dr);
        // for caliculation 
        txtGetQuantity.Text = txtQuantity.Text;
        GridView1.DataSource = dt;
       GridView1.DataBind();
        GridView1.Visible = true;


    }

如果您具有前三列(具有bindvalue)並添加其他兩列,那么這將使您感到困惑。 再次將所有列添加到數據表中。 表示三個新的兩個。

試試吧。 應該可以,因為您的代碼正確。

確保將“自動生成的列”設置為“ true”。

轉到網格視圖的屬性,然后將自動生成的列更改為true(如果為false)。

瀏覽鏈接以進一步闡明: 單擊此處

在我有關參數化查詢和使用'using(){}'語句的評論之后,我將首先編寫如下代碼:

DataTable dt = new DataTable();

using (SqlConnection con = new SqlConnection(myStr))
{
    string sql = "select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode = @txtkey2) or (ItemName is null or ItemName = @txtkey2)";

    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.Add("@txtkey2", txtkey2.Text.Trim());

    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    //You're currently adding a blank DataTable to a session variable below as you've not 
    //filled it yet. I've left this incase that's what you meant to do.
    Session["ItemCode"] = dt;

    con.Open();

    //SqlDataAdapter should be able to fill a DataTable as well as a dataset.
    sda.Fill(dt);

    if (dt.Rows.Count > 0) //Put a breakpoint here to check if anything is returned in dt
    {
        dt.Columns.Add("Quantity");
        dt.Columns.Add("TotalPoints");

        // for caliculation 
        txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();

        //Not sure what you're trying to do here. You're just adding a blank row to dt.
        DataRow dr;
        dr = dt.NewRow();
        dt.Rows.Add(dr);

        // for caliculation 
        txtGetQuantity.Text = txtQuantity.Text;

        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView1.Visible = true;
    }
    else
    {
        //Code to handle nothing being returned.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM