views:

2430

answers:

3

I have a GridView which I am populating by calling a method to return a datatable with two columns. After I return the datatable, I bind it to the gridview and display it.

I am not using a SqlDataSource or an ObjectDataSource, so what is the proper way to Delete a row from the gridview and the underlying data in the database. I just need to delete from one table which is called portfolio. It has 3 columns, ID, which is the unique key, PortfolioID, and PortfolioName. The datatable returns the PortfolioName and the number of items in the Portfolio. I was thinking I could do this in the Row_Deleting event where I would do something like:

DELETE * FROM Portfolio WHERE PortfolioID = @PortfolioID

Am I on the right track and how would I do this? Could I bind the PortfolioID to the GridView DataKey property (What is the correct syntax to do this?)

A: 

I would probably abstract the DB call further out than there. I would probably process the rows after row changes were complete. For postprocessing you can check the rowstate to determine what to do with the data in the row.

CSharpAtl
+2  A: 

I assume that you're deleting a row on a button press. If so, I usually do something like this:

<asp:GridView ID="myGrid" runat="server" DataKeyNames='PortfolioID' OnRowCommand="dgStudent_RowCommand">
    <Columns>
        <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete"></asp:ButtonField>
    </Columns>
</asp:GridView>

Then in your code-behind:

protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // create and open DB connection
    int PortfolioID = (int)myGrid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
    DeleteRow(PortfolioID); // your implementation
    myGrid.DataBind(); 
}

DeleteRow is your implementation of the row delete (maybe the SQL you have above). myGrid is your grid. PortfolioID is the primary key. This also assumes that the only button in your grid is the delete button. If you have more than one button, you'll need to check e.CommandName=="Delete" to make sure you get the delete button.

Keltex
A: 

As i understood you want to delete an row from datagrid view that is filed from database table. This means you want to delete and Table record from some database. The best way to do this is to put an textbox somewhere and creat an event for your datagridview ( on Row select event). This way when you will selct the row you want to delete the id of that roow will be sent to the textbox ( this will be coded). After you have the id of the row you want to delete just add one more button to delete the record in database and bind the datagridview once again.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        this.txt_portfolioId.Text=dataGridView1.CurrentRow.Cells["portfolioID"].Value.ToString();


    }


private void btn_Delete(object sender, EventArgs e)
    {

        //You need to connect to database(regular database connection this connection string and sqlcommand and add this sqlquerry:

Delete * from yourtable where portfolioid = '"+Convert.ToInt32(this.portfolioId.Text)+"';
}

AXheladini