I want to aspxgridview delete that row in the row command.
protected void BootstrapGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridViewRowCommandEventArgs e)
{
if (this.IsPostBack)
{
if (e.CommandArgs.CommandName == "delete")
{
ROW DELETE CODE
}
}
}
You use the ASPxGridViewRowEventArgs.KeyValue property to get the row ID and then delete the specific record from the database. You must specify the KeyFieldName="keyfieldcolumnname" to get the key column value at the RowCommand event.
The ASPxGridViewRowEventArgs.KeyValue property provides a capability to get an ID value by a key value.
Example:
<dxwgv:ASPxGridView ID="gv" runat="server" AutoGenerateColumns="False"
KeyFieldName="ID"...
protected void gv_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e) {
ASPxGridView grid = (ASPxGridView)sender;
object id = e.KeyValue;
//Delete record using data adapter or some other ORM
// Delete from users where userid=id
}
References:
Fields value in ASPxGridView on Row Command
ASPxGridView - How to implement CRUD operations with a custom data source
CRUD Operation Using Stored Procedure In ASP.NET GridView Real Time
Deleting the row from datagridview does not affect the datatable in the database. You need to write a query to remove data from the database..
If you are using sql, mysql etc, try something like getting the primary key or any unique column on the link button event and implement a command to remove the row.
something like this in your click event to get the ID to pass to database
int MyID = int.Parse(yourDataGridView.SelectedRows[0].Cells[0].Value.ToString());
and the query to delete from the database
delete from MyTable
where p_Key = MyID;
Related
I have 2 fields in my SQL table: Name and Status. When I update or change anything from Name on the datagridview and data set. it should update Status as well. How do I do that?
You have many options to do that.
For example:
You can do it in SQL Server using Trigger.
You can do it handling CellValueChanged event of DataGridView.
If you are using typed dataset you can do it overridng OnColumnChanged of the data table.
You can do it handling ColumnChanged event of your data table.
CellValueChanged Example:
Based on your comment as an example for using CellValueChanged:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//Suppose 0 is the index of Name column and 1 is the index of Status Column
//We check if the change is in a datagrid view row and in name column
//Then we change value of Status column.
if (e.RowIndex >= 0 && e.ColumnIndex == 0)
this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "Modified";
}
Or even if you didn't add Status column to the grid you can use such code to change it:
((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Status"] = "Modified";
ColumnChanged Example:
As an example for ColumnChanged:
void table1_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
//Check if the event is raised for Name column
if (e.Column.ColumnName == "Name")
e.Row["Status"] = "Modified";
}
I am working with Aspxgridview with contrxtmenustrip in web application.
In aspxgridview there are 5 columns with some row data. Post right click on grid I need to know which row user has clicked and what is the column name without passing the column index manually in code level.
int rowVisibleIndex = int.Parse(hf["VisibleIndex"].ToString());
By doing this I am able to get the row number. In the same way I need the column name which is selected by user at client side.
gridView.Rows['Index of selected row']['Index or column name of cell you want to get value` from']
that in concrete:
gridView.Rows[gridView.SelectedRowIndex]["ID"]
UPDATE and for column Header use this :
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string headerText = dataGridView1.Columns[e.ColumnIndex].HeaderText;
}
I got a DataGridView which just allows user to delete rows (with no direct editing or adding on DataGridView). I will update my database information whenever user deletes rows on DataGridView (there is no binding among DB and gridview).
I have no problem to have deleting row when user is deleting only one row using DeletingRow event like below:
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
DataGridViewRow row = e.Row; //only single row
}
However i could not capture list of all rows when user selects multiple rows and delete them.
Above event only called once for first row only. In order to handle this case i did set dataGridView1.MultiSelect = false; however this is a workaround! So how can i have list of all selected rows which user is deleting, currently?
All selected rows will be available at dataGridView1.SelectedRows
so you can do something like:
void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
{
this.HandleRowDeletion(row);
}
}
Use dataGridView1.SelectedRows to get a collection (DataGridViewRowCollection type) that contains all the rows selected. Same works for columns (dataGridView1.SelectedColumns) and cells (dataGridView1.SelectedCells).
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedrows.aspx
I'm not sure how to go about this. I am able to pass the Primary key value from the selected row in a GridView to the next page using Response.Redirect, but I need to populate a couple other fields from that row too. Here is what I have. I want to know if I can set another textbox equal to a value using the primary key value, that was passed from the previous page.
protected void Page_Load(object sender, EventArgs e)
{
string dataKey = Request.QueryString["id"];
ProjNo.Text = dataKey;
}
Not altogether clear what you are trying to to do but i can make some observations. Once you are on the new Webform all the context from the original request has gone unless you have saved it somewhere at the time of the Response.Redirect(). You have a few options, putting it into the querystring:
Response.Redirect("/page.aspx?id=" + id + "&field1=" + field1);
You could just save it in the session:
HttpContext.Current.Session["field1"] = field1;
How do I pass the Control ID in a gridview that is derived from a stored procedure into a variable. I will use the variable to pass into the database later on to return some results. Thanks.
var controlId = ((LinkButton)gridView1.Rows[0].FindControl("lbName")).Id;
Are you trying to do something like the above?
Update
You can use the OnSelectedIndex event of the GridView to find the row that was select.
void GridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
var controlId = ((LinkButton)row.FindControl("lbName")).Id;
}
I tend to use the CommandName and CommandArgument fields with LinkButtons in a GridView.
The CommandName represents the name of the command, so that you can have more than one linkbutton that do different things and the CommandArgument is the Database ID or other point of reference for the command.
You need to subscribe to the RowCommand event handler on the GridView