简体   繁体   English

GridView 的刷新值,由 SQL 数据源填充

[英]Refresh value of GridView, which is populated by SQL Data Source

i am populating the values on page load to grid view using SQLDataSource.我正在使用 SQLDataSource 将页面加载时的值填充到网格视图中。 i am able to see all the values properly.我能够正确地看到所有的值。 now i have also added a check box column as template field.现在我还添加了一个复选框列作为模板字段。 So, if i check the check box in front of the row it is removing the that row from database but still i am able to see that same row in grid view.因此,如果我选中该行前面的复选框,它将从数据库中删除该行,但我仍然能够在网格视图中看到同一行。 Now if i refresh the page that values are gone (as the query worked without any error) but i dont want to refresh the page on every entry.现在,如果我刷新页面,值消失了(因为查询工作没有任何错误),但我不想在每个条目上刷新页面。 I want to refresh the grid view on button click.我想在单击按钮时刷新网格视图。 i tried GridView.Update()cand gridview.refresh but both the functions are throwing run time error.我试过 GridView.Update() 和 gridview.refresh 但这两个函数都抛出了运行时错误。 refer below code.参考下面的代码。

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
                            }

                        }
                    }
                }

The last line where your are getting a runtime error出现运行时错误的最后一行

 GridView1.Rows.Remove(2)

change it to将其更改为

GridView1.Rows.RemoveAt(j);

it will work.它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM