带有复选框的Gridview,不起作用...请帮助 [英] Gridview with checkbox,not working... please help

查看:41
本文介绍了带有复选框的Gridview,不起作用...请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GRIDVIEW中的复选框.我要删除所有选中的记录..我在下面有以下代码供您参考...请帮助我使其工作起来...谢谢....

I am working on checkbox in GRIDVIEW. I want to delete all the selected records.. I have this code below for your reference... Please help me to make it work ... Thanks....

protected void btnDelete_Click(object sender, EventArgs e)
{
  string currentrow;
  for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
  {
  CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
  if (cb.Checked)
  {
  currentrow = GridView1.DataKeys[index].Value.ToString();
  string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated    Security=True";
  string query = @"Delete FROM Categories where CategoryID=''" + currentrow + "''";
  SqlConnection con = new SqlConnection(constr);
  SqlCommand cmd = new SqlCommand(query);
  SqlDataAdapter da = new SqlDataAdapter(query, constr);
  DataSet ds = new DataSet();
  da.Fill(ds, "Categories");

  GridView1.DataSource = ds.Tables["Categories"];
  GridView1.DataBind();

  }
 
  }
  }


================================================== =================
对于此代码,我还会得到索引超出范围的错误...我是初学者,请帮助我找到解决此问题的方法....谢谢


[Edited]代码被"pre"标签阻止了[/Edited]


===================================================================
For this code i am also getting an index out of range error... I am a beginner please help me find solution to this problem....Thanks


Code is blocked in "pre" tags[/Edited]

推荐答案

您正在做的事情是为所有人启动一个for循环开头出现的行.
What you are doing is starting a for loop for all the rows that were present at the beginning.
for (int index = 0; index <= GridView1.Rows.Count - 1; index++)


然后,如果找到它,则将其立即删除(可以这样做),然后重新绑定网格(这是错误的).因此,不再选择行,并且减少了计数,因此您肯定会在这里得到错误:


And then, if you find it selected, you remove it immediately(that can be done) and then rebind the grid (this is wrong). Thus, the rows are no more selected and the count is also reduced and thus you are bound to get an error here:

CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");



您所需要做的就是从For循环中删除绑定代码.试试:



All you need is to remove the bind code out of For loop. Try:

protected void btnDelete_Click(object sender, EventArgs e)
{
  string currentrow;
  for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
  {
       CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
       if (cb.Checked)
       {
          // Just delete the selected row and let the loop repeat. 
       }
  }
}



顺便说一句,使用Visual Studio DEBUUGER,它是一个很棒的工具.如果您曾经使用过,自己会找到它的.这是一个逻辑上的错误.



BTW, use Visual Studio DEBUUGER, it''s a great tool. Had you used that you would have find it yourself. It was a logical mistake.


我将从一个问题开始回答.

1.您是否已将CountryID绑定到GridView中?
2.它绑定到什么类型的控件?它是TextBox,Label,DropDownList等吗?如果是,则

I will start the answer with a question.

1. Have you bind the CountryID in the GridView?
2. To what type of control it is binded ? Is it a TextBox, a Label, a DropDownList etc. If so then

protected void clickMe(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
            if (cb.Checked)
            {
                Label lblCountryID = (Label)GridView1.Rows[index].FindControl("lblCountryID");// Assuming that CountryID column is binded to Label control with a name lblCountryID.
                if (!string.IsNullOrWhiteSpace(lblCountryID.Text))
                {
                    string constr = "Data Source=OM;Initial Catalog=TestDB;Integrated Security=True";
                    string nonQuery = "Delete FROM Categories where CategoryID='" + lblCountryID.Text + "'";
                    string query = "Select * Categories";
                    SqlConnection con = new SqlConnection(constr);
                    SqlCommand cmd = new SqlCommand(nonQuery, con);
                    cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter(query, con);
                    da.Fill(dt);
                }
            }
        }

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





希望这能解决您的问题.





I hope this will fix your problem.


您好,

要迭代GridView行项目,可以使用此代码
Hi,

To iterate GridView row items you can use this code
foreach (GridViewRow grv in Gridview1.Rows)
{
   CheckBox chkDelete = grv.FindControl("chkDelete") as CheckBox;
   if (chkDelete.Checked)
   {
     string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated    Security=True";
     string query = @"Delete FROM Categories where CategoryID=''" + Gridview1.DataKeys[grv.DataItemIndex].Value.ToString() + "''";
     SqlConnection con = new SqlConnection(constr);
     SqlCommand cmd = new SqlCommand(query);
     cmd.Connection = con;
     cmd.ExecuteNonQuery();
   }
}


希望这会有所帮助.


Hope this will help.


这篇关于带有复选框的Gridview,不起作用...请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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