How to create a delete button in GridView? - c#

I made another Column in my GridView called delete. When delete is clicked, the row should be deleted or in other words, I need to get the current row's user name to delete it.
Which event should I use? (RowDeleting, Rowdeleted etc...)
How do I get the username from the current row?

Here is a great article about typical usages of DataGrid.
Enjoy.

You can use the RowDeleting event, by storing the user name in the data key collection you can access it programmatically.
<asp:GridView DataKeyNames="UserName" ID="GridView1"
runat="server" OnRowDeleting="GridView1_RowDeleting">
Then, in the code behind use the data key to delete the record.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string userName = (string) GridView1.DataKeys[e.RowIndex].Value;
DeleteUser(userName);
}

1:-
protected void grdLst_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i = Convert.ToInt32(grdLst.DataKeys[e.RowIndex].Value);
ob.DeleteCoverage(i);
Response.Redirect("fullcoverage.aspx");
}
2:-
GridViewRow row = (GridViewRow)grdlist.Rows[e.RowIndex];
string name = row.Cells[1].Text;
Response.Write(name.Trim());

tablename.Rows.RemoveAt(datagrid1.currentcell.rowindex);

Related

how to get the column index of the gridview

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}
It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link

Get data from a specific column when pressing edit on that row in GridView

How do I get data from a specific column that the edit button was pressed on in a gridview?
The following code doesnt work:
protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text;
}
This is because the row is not actually selected. How can I accomplish this?
e.NewEditIndex has the row index of the currently-editing row. You can use that to access the row and read the cell data as necessary.
The parameter GridViewEditEventArgs contains the row index for the current edited row from the gridview.
You should do something like this
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = MyGridView.Rows[e.NewEditIndex].Cells[0].Text;
Another way could be implement a RowCommand event where parameter GridViewCommandEventArgs carries the command name, then you should do something like this:
void MyGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="my command name")
{
//Do stuff here
}
}

Retrieve primary key of the row that clicked on it (edit and delete button)

I have a gridview that shows information from a SqlDatasource. Now the gridview shows some information with an edit button and a delete button at the end of the row.
I want to retrieve the primary key of that row from a table in C#, when I click on the edit or the delete button.
For this I overriden two functions:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Write(e.NewEditIndex);
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write(SqlDataSource1.DeleteCommand);
}
But I cannot retrieve the primary key value. How can I do that?
GridView uses DataKeyNames property, which a collection of key fields. You may use GridView1.DataKeys property to retrieve KeyField value.
Response.Write(GridView1.DataKeys[e.NewEditIndex].Value);
You can access the bound data object liek that:
dataGridView1.Rows[e.NewEditIndex].DataBoundItem
You must then cast it to your data type and you can access all properties.
e.NewEditIndex
Gives you the index of row being edited not the key.
To retrive key,get the value from control of the index row
Dim gvRow As GridViewRow = lb.BindingContainer ‘Getting current row to get index
Dim strKey As String = GridView1.DataKeys[gvRow.RowIndex][0].ToString()

Select button in a grid view

<asp:GridView ID="GridView1" autogenerateselectbutton="True" selectedindex="0"
autogeneratecolumns="True" allowpaging="true" runat="server" CssClass="style39"
datakeynames="Email" RowCommand="GridView1_RowCommand" ShowSelectButton="True">>
I have this select button in my grid view. I have a delete button in the page. I want to delete the selected row but how can I pass the value to the delete function so that the I can write a code to delete the selected row in the database. I want to know which row is selected how to pass the value.
My grid view has a column called email and user name. I want to pass the selected row email. Thanks
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "CommandName")
{
String Email = e.CommandArgument.ToString(); // will Return current Row primary key value
MySqlConnection connectionString = new MySqlConnection("Server=127.0.0.1;Database=surelyknown;Uid=root");
connectionString.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand();
command = new MySqlCommand("DELETE from tbl_group, tbl_usergroups using tbl_group inner join tbl_usergroups where tbl_group.GroupID =#Email And tbl_usergroups.tbl_group_GroupID =#Email", connectionString);
command.Parameters.Add("#Email", MySqlDbType.VarChar, 25);
command.Parameters["#Email"].Value = Email;
use RowCommand event of Gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "CommandName")
{
String Email = e.CommandArgument.ToString(); // will Return current Row primary key value
//..Put deletion code here....
//.....
}
}
If you are not going to delete multiple rows at the same time on the gridview a separate delete button is not needed
You can simply place the delete button inside a template field in your gridview. That way, you won't need to select a gridview row then press the delete button on your page(2 steps)
You can then write a click event for that button & fetch the current gridview row's current index by either using DataKeys or e.CommandArgument
Let me know if you need any more help with this.

On Button1_Click need to get value from selected DataKey (User presses Select in gridview and highlights row)

This is kiling me. On Button1_Click I need to get the value from selected DataKey (User presses Select in gridview and highlights row)..Can anyone help? Thank You..
protected void Button1_Click(object sender, EventArgs e)
{
string id = GridView1.SelectedDataKey.Value.ToString();
Label1.Text = id;
}
You haven't mentioned what's not working, do you get any errors?
To retrieve the SelectedDataKey you have to tell the GridView what's the primary key column of your datasource via it's property DataKeyNames.
You could also use the GridView's SelectedValue property.
If all does not work, you should provide source code. Maybe you are binding the GridView on every postback.

Categories