1

您知道如何通过单击编辑按钮来编辑 GridView 上的记录。我已经对我的代码进行了编程,当单击 GridView 上的编辑按钮时,会弹出一个新窗口,其中包含各个字段中给出的记录的所有详细信息。

我的程序中也有搜索功能。当用户在搜索页面的给定字段中输入一些参考信息后单击搜索按钮时,所需的记录或记录(如果多于 1 个)显示在 GridView 列表中搜索页面的底部。当然,如果用户想要编辑搜索记录,请使用编辑按钮选项。

您能否帮助我指导如何在网格视图中编辑搜索记录,因为当我单击编辑以更改搜索记录时,我的程序会打开数据库表中所有记录列表中的第一条记录(并且不会打开我点击编辑的记录)在一个新窗口中。

我的编辑行记录代码是:

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        //BindGridView();
        lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
        MultiView1.SetActiveView(vRecord);

        btnInsert.Visible = false;
        //this.lblMedium.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;


        using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                String sql = "select [DocumentsID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Attachment],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response] from dbo.Documents where [DocumentsId]=N'" + GridView1.Rows[e.NewEditIndex].Cells[1].Text + "'";
                cmd.Connection = con;
                cmd.CommandText = sql;
                con.Open();

                //SqlDataAdapter da = new SqlDataAdapter(sql,con);
                //DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                {
                    adp.Fill(ds);

                }

                this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString();
                this.txtSubject.Text = ds.Tables[0].Rows[0][2].ToString();
                this.ddlSource.Text = ds.Tables[0].Rows[0][3].ToString();
                this.ddlDestination.Text = ds.Tables[0].Rows[0][4].ToString();
                this.ddlMedium.Text = ds.Tables[0].Rows[0][5].ToString();
                this.txtDatePrinted.Text = ds.Tables[0].Rows[0][6].ToString();
                this.txtDateReceived.Text = ds.Tables[0].Rows[0][7].ToString();
                this.ddlDocumentType.Text = ds.Tables[0].Rows[0][8].ToString();
                this.cbxAction.Checked = ds.Tables[0].Rows[0][9].Equals(cbxAction.Checked);
                this.txtDueDate.Text = ds.Tables[0].Rows[0][10].ToString();
                this.txtActualDate.Text = ds.Tables[0].Rows[0][11].ToString();
                this.txtContent.Text = ds.Tables[0].Rows[0][12].ToString();
                this.txtTag.Text = ds.Tables[0].Rows[0][13].ToString();
                this.txtIssue.Text = ds.Tables[0].Rows[0][14].ToString();
                this.txtAttachment.Text = ds.Tables[0].Rows[0][15].ToString();
                this.txtNotes.Text = ds.Tables[0].Rows[0][16].ToString();
                this.ddlAssignedTo.Text = ds.Tables[0].Rows[0][17].ToString();
                this.txtReplyRef.Text = ds.Tables[0].Rows[0][18].ToString();
                this.ddlPriority.Text = ds.Tables[0].Rows[0][19].ToString();
                this.ddlStatus.Text = ds.Tables[0].Rows[0][20].ToString();
                this.ddlResponse.Text = ds.Tables[0].Rows[0][21].ToString();


                if (con != null)
                {
                    con.Close();
                }

搜索功能的代码是:

   protected void Search_Click(object sender, EventArgs e)
   {

       String sql = "select * from PIMS.dbo.Documents";
       bool flag = false;
       if (!txtRef0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Ref =N'" + txtRef0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Ref =N'" + txtRef0.Text + "'";
           }
       }

       if (!ddlDst.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Dst =N'" + ddlDst.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Dst =N'" + ddlDst.Text + "'";
           }
       }

       if (!ddlSrc.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Src =N'" + ddlSrc.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Src =N'" + ddlSrc.Text + "'";
           }
       }

       if (!txtSubject0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Subject =N'" + txtSubject0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Subject =N'" + txtSubject0.Text + "'";
           }
       }
       if (!ddlMedium0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Medium =N'" + ddlMedium0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Medium =N'" + ddlMedium0.Text + "'";
           }
       }

       if (!ddlDocumentType0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Document_Type =N'" + ddlDocumentType0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Document_Type =N'" + ddlDocumentType0.Text + "'";
           }
       }
       if (!txtContent0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Content  LIKE N'%" + txtContent0.Text + "%'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Content  LIKE N'%" + txtContent0.Text + "%'";
           }
       }
       if (!txtTag0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Tag  = N'" + txtTag0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Tag  =N'%" + txtTag0.Text + "'";
           }
       }
       if (!txtIssue0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Issue_No= N'" + txtIssue0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Issue_No  LIKE N'%" + txtIssue0.Text + "'";
           }
       }

       if (!txtNotes0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Notes LIKE N'%" + txtNotes0.Text + "%'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Notes  LIKE N'%" + txtNotes0.Text + "%'";
           }
       }

       if (!ddlAssignedTo0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Assigned_To = N'" + ddlAssignedTo0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Assigned_To  = N'" + ddlAssignedTo0.Text + "'";
           }
       }

       if (!ddlPriority0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Priority = N'" + ddlPriority0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Priority  = N'" + ddlPriority0.Text + "'";
           }
       }

       if (!ddlStatus0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Status = N'" + ddlStatus0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Status  = N'" + ddlStatus0.Text + "'";
           }
       }

       if (!ddlResponse0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Response = N'" + ddlResponse0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Response = N'" + ddlResponse0.Text + "'";
           }
       }

       if (!txtReplyRef0.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Reply_Ref = N'" + txtReplyRef0.Text + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Reply_Ref = N'" + txtReplyRef0.Text + "'";
           }
       }

       if (!txtDatePrintedF.Text.Equals("") && !txtDatePrintedT.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Date_Printed >=N'" + DateTime.ParseExact(txtDatePrintedF.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Date_Printed <=N'" + DateTime.ParseExact(txtDatePrintedT.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
           }
       }
       if (!txtDateReceivedF.Text.Equals("") && !txtDateReceivedT.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Date_Received >=N'" + DateTime.ParseExact(txtDateReceivedF.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Date_Received <=N'" + DateTime.ParseExact(txtDateReceivedT.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
           }
       }
       if (!txtDueDateF.Text.Equals("") && !txtDueDateT.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Due_Date >=N'" + DateTime.ParseExact(txtDatePrintedF.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Due_Date <=N'" + DateTime.ParseExact(txtDatePrintedT.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
           }
       }
       if (!txtActualDateF.Text.Equals("") && !txtActualDateT.Text.Equals(""))
       {
           if (flag == false)
           {
               sql = sql + " where Actual_Date >=N'" + DateTime.ParseExact(txtDatePrintedF.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
               flag = true;

           }
           else
           {
               sql = sql + "  and Actual_Date <=N'" + DateTime.ParseExact(txtDatePrintedT.Text, "G", CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd") + "'";
           }
       }

       txtSQL.Text = sql;

       using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
       {
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.Connection = con;
                cmd.CommandText = sql+";";
                //cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                //dataset object to get all select statement results
                DataSet ds = new DataSet();

                //sql dataadoptor to fill dataset
                using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                {
                    //here all select statements are fill in dataset object
                    adp.Fill(ds);

                } this.GridView1.DataSource = ds.Tables[0]; // first select statement result
                this.GridView1.DataBind();


            }

            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }

            txtSQL.Visible = false;
            MultiView1.SetActiveView(vGrid);
       }
   }
4

2 回答 2

2

您不是在检索您选择的行,而是检索您的datagridview. 你的代码:

this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString(); //you are referring to DataSet
                                                       //first row, which will always be same

你只需要改变一件事:

设置一个变量:

int SelectedRowIndex = GridView1.SelectedIndex; //it will return int value of selected row

然后用和替换代码中的0索引:RowsSelectedRowIndexds.Tables[0]GridView1

this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString(); //your code
this.txtRef.Text = GridView1.Rows[SelectedRowIndex].Cells[0].Text.ToString();  //corrected one
//do this for your all 21 cells by changing cell indexes

现在它将从您的gridviewnot 中选择行DataSet

笔记:

我已经更改了整个答案,因为您之前没有提到您使用的是Web Controls,而不是Windows Form Controls

额外的:

这是检查任何条件的方法:

if(SelectedRowIndex >= 0)
{
    GridView1.Rows[SelectedRowIndex].Cells[0].Text.ToString();
    //and so on
}
于 2013-07-02T06:53:08.077 回答
0

难道不能简单地说gridview.editindex = e.neweditindex

或者在其他情况下,如果您使用弹出窗口,那么我认为它不是触发行编辑命令的理想方式,因为您没有使用它的好处

反正

如果可以将键传递给您的查询以获取特定记录,那么您可以从编辑索引行中找到数据键值,然后将该键与您的查询一起传递,它将仅返回该特定记录,

于 2013-07-02T12:31:34.227 回答