【问题标题】:Refresh value of GridView, which is populated by SQL Data SourceGridView 的刷新值,由 SQL 数据源填充
【发布时间】:2020-07-14 19:36:31
【问题描述】:

我正在使用 SQLDataSource 将页面加载到网格视图中的值填充。我能够正确查看所有值。现在我还添加了一个复选框列作为模板字段。因此,如果我选中该行前面的复选框,它将从数据库中删除该行,但我仍然能够在网格视图中看到同一行。现在,如果我刷新值消失的页面(因为查询没有任何错误),但我不想刷新每个条目的页面。我想在按钮单击时刷新网格视图。 我尝试了 GridView.Update() 和 gridview.refresh,但是这两个函数都引发了运行时错误。 参考下面的代码。

int i = 0;
            int j = 0;
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox CH = (CheckBox)GridView1.Rows[i].FindControl("CheckBox4");
                Label DebitSrNo = (Label)GridView1.Rows[i].FindControl("Label2");
                Label DebitDate = (Label)GridView1.Rows[i].FindControl("lblDebitDate");

                if (CH.Checked == true)
                {
                    for (j = 0; j < GridView2.Rows.Count; j++)
                    {
                        CheckBox CreditCheckBox = (CheckBox)GridView2.Rows[j].FindControl("chkCreditBalance");
                        Label lb1 = (Label)GridView2.Rows[j].FindControl("lblSrNoCredit");
                        Label CreditDate = (Label)GridView2.Rows[j].FindControl("lblCreditDate");
                        if (CreditCheckBox.Checked == true)
                        {
                            if ((Convert.ToDateTime(CreditDate.Text) > Convert.ToDateTime(DebitDate.Text)))
                            {
                                string query = "UPDATE [BALANCE_CREDIT] SET STATUS = 1 WHERE SR_NO = {sr_no}";
                                query = query.Replace("{sr_no}", lb1.Text);
                                SqlConnection conn = OpenDBConnection();
                                int AffectedRows = 0;
                                SqlCommand cmd = new SqlCommand(query, conn);
                                AffectedRows = cmd.ExecuteNonQuery();
                                query = "UPDATE [BALANCE_DEBIT] SET STATUS = 1 WHERE SR_NO = {sr_no}";
                                query = query.Replace("{sr_no}", DebitSrNo.Text);
                                cmd = new SqlCommand(query, conn);
                                AffectedRows = cmd.ExecuteNonQuery();
                                CloseConnection(conn);
                                GridView1.Rows.Remove(2);//here it is giving runtime error. so as for update and refresh
                            }

                        }
                    }
                }

【问题讨论】:

  • 毫无疑问,您将在线上遇到问题……GridView1.Rows.Remove(2);……问题是这行代码在……for (i = 0; i &lt; GridView1.Rows.Count; i++)……循环“内部”! ...除非代码对i 索引进行某种“特殊”处理(不推荐)...如果您“删除”某些行,循环将在某处失败。如果您必须删除这些行......那么我建议跟踪这些行,然后在循环“外部”删除它们。

标签: c# asp.net web gridview datagridview


【解决方案1】:

您遇到运行时错误的最后一行

 GridView1.Rows.Remove(2)

改成

GridView1.Rows.RemoveAt(j);

它会起作用的。

【讨论】:

  • 正如您的代码所说,有两个 datagridview。您能否确认您要在哪个gridview 中删除记录?
  • 如果你想在 gridview1 中删除,那么这段代码可以工作 GridView1.Rows.RemoveAt(i);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
相关资源
最近更新 更多