Jak wykonać Usuń i aktualizuj rekordy w tabeli SQL z widoku siatki w C #. Próbowałem go na wiele sposobów, ale nie działa w żaden sposób. Pomóż mi rozwiązać ten problem, aby dodać usunięcie i edycję funkcjonalności do mojego widoku siatki do edycji mojej tabeli SQL.

 protected void userlist_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
            userlist.DeleteRow(e.RowIndex);

    }
    protected void userlist_RowEditing(object sender, GridViewEditEventArgs e)
    {
        userlist.EditIndex = e.NewEditIndex;
        DataBind();
    }

    protected void userlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection();
        try
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["PMS"].ToString();

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

           // SqlCommand cmd = "UPDATE  UserDT SET (UserId,UserName, Password, Email, PhoneNumber )" + "VALUES ('"+ UserName.Text +"' + tbxPassword.Text + tbxPermission.Text + "');";
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
           Note.Text = ex.Message;
        }
    }
0
ROHIT MANDALAPU 17 luty 2017, 16:00

2 odpowiedzi

Najlepsza odpowiedź

Możesz użyć, jak pokazano poniżej. W przypadku wkładki z tagów kolumn projektowych Gridview

<Columns>   
    <FooterTemplate>
    <asp:LinkButton ID="btnAddNew" Text="Add New" runat="server" CommandName="AddNew" ToolTip="ADD"/>
    </FooterTemplate>
    </asp:TemplateField>
</Columns> 

protected void EmpGrid_Command(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("AddNew"))
    {
        TextBox txtf = (TextBox)EmpGrid.FooterRow.FindControl("txtFooterFname");
        TextBox txtl = (TextBox)EmpGrid.FooterRow.FindControl("txtfooterLname");
        TextBox txtq = 
                (TextBox)EmpGrid.FooterRow.FindControl("txtfooterqualification");
        TextBox txtd = 
                (TextBox)EmpGrid.FooterRow.FindControl("txtfooterdecription");
        TextBox txts = (TextBox)EmpGrid.FooterRow.FindControl("txtfootersalary");

        // For Inserting New Row
        string insrtquery = "insert into EMPTable
                            (fname,lname,qualification,designation,sal) values
                            ('" + txtf.Text + "','" + txtl.Text + "',
                             '" + txtq.Text + "','" + txtd.Text + "','" +
                                  txts.Text + "')";
        da = new SqlDataAdapter(insrtquery, con);
        DataSet ds = new DataSet();
        da.Fill(ds,"inserted");
        Bindemployees();
     }       
}

protected void EmpGrid_Updating(object sender, GridViewUpdateEventArgs e)
{
int empid = Convert.ToInt32(EmpGrid.DataKeys[e.RowIndex].Value.ToString());
string fname = EmpGrid.DataKeys[e.RowIndex].Values["fname"].ToString();
string lname = EmpGrid.DataKeys[e.RowIndex].Values["lname"].ToString();
TextBox txtq = 
       (TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditqualification");
TextBox txtd = 
       (TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditdesignation");
TextBox txts = 
       (TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditsalary");

string updatequery = "update EMPTable set qualification='" + txtq.Text +
                   "',designation='" + txtd.Text + "',sal='" + txts.Text + "'
                   where empid='" + empid + "'";
DataSet ds = new DataSet();
da = new SqlDataAdapter(updatequery,con);
da.Fill(ds,"added");
EmpGrid.EditIndex = -1;
Bindemployees();
}

protected void EmpGrid_Deleting(object sender, GridViewDeleteEventArgs e)
{
int empid = Convert.ToInt32(EmpGrid.DataKeys[e.RowIndex].Value.ToString());
da = new 
SqlDataAdapter("delete from EMPTable where empid='" + empid + "'", con);
DataSet ds = new DataSet();
da.Fill(ds,"deleted");
Bindemployees();
}

Aby lepiej zrozumieć, zobacz ten link

http://reddyninfosoft.blogspot.in/2012/07/insert-edit-update-and-delete-with-in.html.

0
Vara 18 luty 2017, 10:20

Wiążysz z datatable. Spróbuj tego:

DataTable sourceData = (DataTable)userlist.DataSource;
sourceData.Rows[e.RowIndex].Delete();
userlist.DataSource = sourceData;
userlist.DataBind();
0
Sudhakar Rao 17 luty 2017, 22:10