使用复选框asp.net显示网格视图行中的选定值

时间:2016-06-11 07:16:52

标签: c# asp.net checkbox

我的代码中有什么问题吗?我想在gridview中显示第8行的值。我有这个代码,它不适合我。没有错误,但它没有显示我期待看到的价值。

protected void Button1_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        string strname = string.Empty;
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkRow");
            if (chk != null & chk.Checked)
            {
                str += GridView1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
                strname += gvrow.Cells[8].Text + ',';

            }
        }
        strname = strname.Trim(",".ToCharArray());
        lblRecord.Text = "<b>Credit Request: </b>" + strname;
    }

尝试调试它。它根本没有获得细胞价值。我在这里失踪了什么?

updated output

UPDATE:

让它变得有效。它的主页原因导致它无法正常工作。这是我的代码:

protected void btmDisplay_Click(object sender, EventArgs e)
    {
        //string data = "";
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
                if (chkRow.Checked)
                {
                    using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
                    {
                        scn.Open();
                        SqlCommand cmd = new SqlCommand(@"UPDATE UserData SET CreditRequest = CAST(REPLACE(c.CreditRequest, ',', '') as int) FROM CreditRequests c INNER JOIN Userdata u on c.username=u.username Where c.Username=@Username", scn);
                        cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];

                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        lblmsg.Text = "Approved";

我现在想要发生的是当单击该复选框时,该行(特别是row8)将在我的sql数据库上更新。我已经尝试了上面的代码,但它给了我一个错误:

参数化查询&#39;(@ Username nvarchar(4000))UPDATE UserData SET CreditRequest = CA&#39;需要参数&#39; @ Username&#39;,这是未提供的。

调试更新:

debug

更新2:

我试过这个

SqlCommand cmd = new SqlCommand("Update UserData set CreditRequest = '" + row.Cells[8].Text + "' where Username=@Username", scn);
                        cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = row.Cells[1].Text;

我的错误是 转换varchar值&#39; 5,000

时转换失败

2 个答案:

答案 0 :(得分:0)

使用它

foreach(GridView1.Rows中的GridViewRow行)         {             if(row.RowType == DataControlRowType.DataRow)             {                 System.Web.UI.WebControls.CheckBox chkrow =(row.Cells [0] .FindControl(&#34; chkrow&#34;)as System.Web.UI.WebControls.CheckBox);                 if(chkrow.Checked)                 {                    strname = GridView1.Rows [row.RowIndex] .Cells [1] .Text.ToString();                 }             }         }

答案 1 :(得分:0)

如果您绑定gridview上的Page_Load,请确保已设置if(!IsPostBack)

if(!IsPostBack)
{
//BindGridView here
}

试试这个

cmd.Parameters.AddWithValue("@Username", Session["New"].ToString());

更新

删除CreditRequest值

附近的单个qoutes
SqlCommand cmd = new SqlCommand("Update UserData set CreditRequest = " + row.Cells[8].Text + " where Username=@Username", scn);