Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
you know how you are able to Edit a record on a GridView by clicking Edit button. I have programmed my code in such a way that when clicks Edit button on GridView, a new window pops up with all the details of the record given in respective fields.

I also have search-feature in my program. That when a user clicks search button after entering some referenced information in the given fields on search page, the desired record or records (if more than 1) show up at the bottom of the search page in a GridView list. Ofcourse with Edit button option in case user wants to Edit the search-record.

could you please help me in guiding how to edit a searched record in a gridview because when when I click Edit to change searched-record,my program opens the first record from the list of all records in the database table (and doesn't open the record that I clicked on to EDIT) in a new window.

My code for edit row record is:

<pre lang="c#">

        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();
                }



Code for Search Function is:

<pre lang="c#">

   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);
       }
   }


Help please.
Posted
Comments
BulletVictim 2-Jul-13 6:00am    
Not sure exactly what you are looking for. But what you might be looking for is something in the lines of:
Gridview1.SelectedRows[0].Cells[i].Value.ToString()
To find the correct record you want to edit
[no name] 5-Jul-13 1:55am    
Thanks for the response. I am sorry I couldnt make things clear in my question. I will give it a shot again.
I have a gridview on my webform .. which is linked to sql database .. all records from sql database are populated in the gridview ... now i have search-feature on my webform ..with some fields for the user to enter the information about the record he/she wants to search ... after user enters that info. and press search .. gridview now shows only the records that match with the search .. and do not show the whole list of records from sql server... now when user clicks on EDIT or DELETE button placed right before the records ... (searched-records) my program exits to the main window (a different view) and no editing can be done on the search record. On the other hand .. if you do not search for any record ... and edit a record that is in the gridview .. linked to the database .. you can easily edit and update it. It is only when you try to edit a searched-record (which is binded with the gridview) editing is not happening.

Please assist if you followed.
Thanks.
BulletVictim 9-Jul-13 9:31am    
If you don't mind could you share the update statement and the edit and delete button code?
[no name] 10-Jul-13 8:35am    
Update code:
` protected void btnUpdate_Click(object sender, EventArgs e)
{

MultiView1.SetActiveView(ViewGrid);

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


using (SqlCommand cmd = new SqlCommand())
{

string sql = "UPDATE dbo.Dwg_Register SET Ref = @Ref, Dwg_Ref = @Dwg_Ref,Title = @Title, Dwg_Received_Date = @Dwg_Received_Date, Rev = @Rev, Trade = @Trade, Type=@Type,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, Status=@Status,Action_Date=@Action_Date from dbo.Dwg_Register where DwgRegID=@DwgRegID";

cmd.Connection = con;
cmd.CommandText = sql;

cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));
cmd.Parameters.Add(new SqlParameter("@Dwg_Ref", txtDwgRef.Text));
cmd.Parameters.Add(new SqlParameter("@Title", txtTitle.Text));
cmd.Parameters.Add(new SqlParameter("@Dwg_Received_Date", txtDwgReceivedDate.Text== "" ? DBNull.Value : (object)txtDwgReceivedDate.Text));
cmd.Parameters.Add(new SqlParameter("@Rev", txtRev.Text));
cmd.Parameters.Add(new SqlParameter("@Trade", ddlTrade.Text));
cmd.Parameters.Add(new SqlParameter("@Type", ddlType.Text));// == "" ? DBNull.Value : (object)txtDateReceived.Text));
cmd.Parameters.Add(new SqlParameter("@Produced_Date", txtProducedDate.Text == "" ? DBNull.Value : (object)txtProducedDate.Text));
cmd.Parameters.Add(new SqlParameter("@Produced_By", ddlProducedBy.Text ));
cmd.Parameters.Add(new SqlParameter("@Submittal_Ref", txtSubmittalRef.Text));
cmd.Parameters.Add(new SqlParameter("@Issued_To", ddlIssuedTo.Text));
cmd.Parameters.Add(new SqlParameter("@Date_Issued", txtDateIssued.Text == "" ? DBNull.Value : (object)txtDateIssued.Text));
cmd.Parameters.Add(new SqlParameter("@Purpose", ddlPurpose.Text));
cmd.Parameters.Add(new SqlParameter("@Status", ddlStatus.Text));
cmd.Parameters.Add(new SqlParameter("@Action_Date", txtActionDate.Text == "" ? DBNull.Value : (object)txtActionDate.Text));
cmd.Parameters.Add(new SqlParameter("@DwgRegID", int.Parse(lblSet.Text)));

con.Open();

cmd.ExecuteNonQuery();
}
if (con.State == ConnectionState.Open)
{
con.Close();
}
SqlConnection con1 = new SqlConnection("Data Source= MEHDI-PC\\SQLExpress; Initial Catalog=PIMS; Integrated Security=True");
string sql1 = "SELECT * from dbo.Dwg_Register";
con.Open();

SqlDataAdapter da = new SqlDataAdapter(sql1, con1);
DataSet ds1 = new DataSet();
da.Fill(ds1);
if (con != null)
{
con.Close();
}
GridView1.DataSource = ds1;
GridView1.DataBind();




lblUpdate.Text = "Record updated sucessfully.";

// if (!Page.IsPostBack)
// Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "<script language=javascript>document.getElementById('" + btnRefresh.ClientID + "').click();</script>");

}

Edit code:

`protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
MultiView1.SetActiveView(ViewRecord);

lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
int index = e.New
[no name] 10-Jul-13 8:42am    
continued...

Edit code:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
MultiView1.SetActiveView(ViewRecord);

lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
int index = e.NewEditIndex + 1;
lblIndex.Text = index.ToString();

btnSave.Visible = false;
btnUpdate.Visible = true;

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

string sql = "select [DwgRegID],[Ref],[Dwg_Ref],[Title],[Dwg_Received_Date],[Rev],[Trade],[Type],[Produced_Date],[Produced_By],[Submittal_Ref],[Issued_To],[Date_Issued],[Purpose],[Status],[Action_Date] from dbo.Dwg_Register where [DwgRegID]=N'" + GridView1.Rows[e.NewEditIndex].Cells[1].Text + "'";
con.Open();

SqlDataAdapter adp = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
adp.Fill(ds);

this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString();
this.txtDwgRef.Text = ds.Tables[0].Rows[0][2].ToString();
this.txtTitle.Text = ds.Tables[0].Rows[0][3].ToString();
this.txtDwgReceivedDate.Text = ds.Tables[0].Rows[0][4].ToString();
this.txtRev.Text = ds.Tables[0].Rows[0][5].ToString();
this.ddlTrade.Text = ds.Tables[0].Rows[0][6].ToString();
this.ddlType.Text = ds.Tables[0].Rows[0][7].ToString();
this.txtProducedDate.Text = ds.Tables[0].Rows[0][8].ToString();
this.ddlProducedBy.Text = ds.Tables[0].Rows[0][9].ToString();

this.txtSubmittalRef.Text = ds.Tables[0].Rows[0][10].ToString();
this.ddlIssuedTo.Text = ds.Tables[0].Rows[0][11].ToString();
this.txtDateIssued.Text = ds.Tables[0].Rows[0][12].ToString();
this.ddlPurpose.Text = ds.Tables[0].Rows[0][13].ToString();
this.ddlStatus.Text = ds.Tables[0].Rows[0][14].ToString();
this.txtActionDate.Text = ds.Tables[0].Rows[0][15].ToString();


if (con != null)
{
con.Close();
}
btnSearch.Visible = false;
btnBacktoHome.Visible = true;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900