I am implementing the bulk delete functionality with the help of Checkbox. But when I call the ID like below
string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
I get the error as
System.EventArgs does not contain a definition of RowIndex.
I dont know why it is happening. Please see my code for your reference:-
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in grdUser.Rows)
{
CheckBox chkDelete = (CheckBox)grdUser.FindControl("chkDelete");
if (chkDelete.Checked)
{
string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
}
}
}
Do let me know what changes I have to make
You should add the gvRow.RowIndex as stated by Sandeep.
Then you have to bind your gridview somewhat like this.
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in grdUser.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
CheckBox chkDelete = (CheckBox)gvRow.FindControl("chkDelete");
if (chkDelete.Checked)
{
string Id = grdUser.DataKeys[gvRow.RowIndex].Value.ToString();
DeleteRecordByID(Id);
}
}
}
//Bind your Gridview here
}
Let me know if it works or not
you can Use Below Code
for (int i = 0; i < grdUser.Rows.Count; i++)
{
//Your logic and use grdUser.DataKeys[i].Value.ToString();
for delete
}
Related
I have a grid view and in this grid some rows have a field called "Department". What I want is a code that reads that fields value and then if it equals a string "Industrial" the row should be hidden and not shown.
I tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (e.Row.Cells[4].Text == "industrial")
e.Row.Visible = false;
}
But it keeps saying (e) is not defined, and there is no such thing as (.Row).
It's GridViewRowEventArgs that have access to e.Row but it's used in RowCreated and RowDataBound events. You can use SelectedRow property of GridView instead.
var gridView = (GridView)sender;
if (gridView.SelectedRow.Cells[4].Text == "industrial")
gridView.SelectedRow.Visible = false;
Updated:
To hide the rows when page is loaded, place the for loop inside the Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
// ...
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[4].Text == "industrial")
GridView1.Rows[i].Visible = false;
}
}
I am trying to hide a row when its button clicked in my gridview. Does anyone have a solution for this, I am all yours,
Code behind:
protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "removethis")
{
Guid id = Guid.Parse(e.CommandArgument.ToString());
UsersBL.DeleteUserByUserId(id);
}
else
{
//hide the row
}
}
try this - Add RowDataBound event and do like this:
e.Row.Visible = false;
You need to access the row-object and then you can hide it, it's a little tricky:
protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "removethis")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
row.Visible = false;
}
}
I have created a datagridview that gets all the data from MySQL Workbench database. Below this datagridview, I have created another datagrid with the same columns. For that, I have created a copy function, that on selecting the rows form the first datagridview, copies the selected rows to the second datgrid.
I have then created textboxes that displays the rows that are selected in the second datagridview.
All I want to do now is, get the values from the textboxes, match it in first datagridview, and after clicking on delete button, delete the respected row from first datagridview and respectievely from the database.
I am new to c#, so any help will be appreciated.
Source Code:
namespace panelApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void copy_Click(object sender, EventArgs e)
{
// dataGridView2.Rows.Clear(); (code used if you want to delete previous selections)
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (item.Selected == true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = item.Cells[0].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
}
}
}
public void fetch_Click(object sender, EventArgs e)
{
this.productsTableAdapter.Fill(this.productsDataset.products);
productsDataset dt = new productsDataset();
foreach (DataRow item in dt.products.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[1].Value = item["product_id"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["product_name"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["category_id"].ToString();
}
}
private void delete_btn_Click(object sender, EventArgs e)
{
try
{
if (dataGridView1.Rows.Cell[0].Text == TextBox1.tex) // error on this line.
{
}
}
catch (System.Exception)
{
MessageBox.Show("Not able to Delete");
}
}
private void dataGridView2_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView2.Rows[e.RowIndex];
product_idTxtbx.Text = row.Cells["product_id"].Value.ToString();
proName_txtbx.Text = row.Cells["product_name"].Value.ToString();
catID_txtbx.Text = row.Cells["category_id"].Value.ToString();
}
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView2.Rows[e.RowIndex];
product_idTxtbx.Text = row.Cells["product_id"].Value.ToString();
proName_txtbx.Text = row.Cells["product_name"].Value.ToString();
catID_txtbx.Text = row.Cells["category_id"].Value.ToString();
}
}
private void delete2_Click(object sender, EventArgs e)
{
// if (dataGridView1.Rows["row.index"].Cells["productidDG"].Value.ToString() == product_idTxtbx.Text)
{
}
}
}
}
On delete button click event btn_OnDelete() find the gridview index then find the all id's by row index.
Then compare each gridview row id to textbox item id
like:
if(GridView1.Rows.Cell[0].Text == TextBox1.tex)
{
// Delete Query here
}
for full solution share code with me.
see here demo link: CRUD Operation link
You should use this code:
<asp:Button ID="delete" runat="server" CommandArgument='<%#Eval("Id") %>' Text="Delete" />
private void delete_btn_Click(object sender, EventArgs e)
{
//incase you need the row index
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
int Prod_Id= Convert.ToInt32(e.CommandArgument);
//followed by your code
try
{
// Then Compare your Id here in this if condition
// if (dataGridView1.Rows.Cell[0].Text == TextBox1.tex
if (Prod_id == Convert.ToInt32(TextBox1.text)) // error on this line.
{
// User Code Here
}
}
catch (System.Exception)
{
MessageBox.Show("Not able to Delete");
}
}
please make your code like below
private void delete_btn_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in dataGridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (row.Cell[0].value == TextBox1.tex)
{
//delete opration here for TextBox1.tex/row.Cell[0].Text(Product_id)
break;
}
}
}
}
catch (System.Exception)
{
MessageBox.Show("Not able to Delete");
}
}
I have a gridiview which gives me a output of 20 members in a list. Now i want to pay salary to only specific person and to only to those persons whose checkbox is checked i tried some thing like this a follows :
protected void Button_Click(object sender, EventArgs e)
{
foreach (GridViewRow GVR in GridView.Rows)
{
if (GVR.RowType == DataControlRowType.DataRow)
{
CheckBox c = (CheckBox)GVR.FindControl("MemberCheck");
if (c.Checked)
{
string DividendAmount = GridView.Rows[0].Cells[5].Text;
string MOP = GridView.Rows[0].Cells[4].Text;
}
}
}
}
But the problem is by this code i can access only any one particular row but what if i have selected n rows ???
Don't you mean?
string DividendAmount = GVR.Cells[5].Text;
string MOP = GVR.Cells[4].Text;
I have a gridview that has linkbuttons that call modalpopups and textboxes with values. I am trying to implement sorting for the gridview, but the if(!ispostback) statement I need for sorting prevents the modalpopup from appearing. It also does not sort the textboxes in the gridview. Is there a way to implement sorting without using ispostback in the page_load?
Here is the code for the modalpopup, gridview binding and sorting.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sortOrder"] = "";
Bind_Gridview("", "");
loadModals();
}
}
protected void viewModal(object sender, EventArgs e)
{
...
mainPanel.Controls.Add(exstModal);
mainPanel.Controls.Add(exstModalBox);
exstModalBox.Show();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
Bind_Gridview(e.SortExpression, sortOrder);
}
public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}
return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}
protected void gv1_RowCommand(object sender, GridViewRowEventArgs e)
{
...
CheckBox cb = new CheckBox();
TextBox ca = new TextBox();
ca.Width = 20;
TextBox cga = new TextBox();
cga.Width = 20;
if (e.Row.RowType == DataControlRowType.DataRow) //Foreach row in gridview
{
while (dr1.Read())
{
ca.Text = dr1["cyla"].ToString();
cga.Text = dr1["cga"].ToString();
checkText = dr1["completed"].ToString();
if (checkText == "True")
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
...
dr1.Close();
conn1.Close();
e.Row.Cells[6].Controls.Add(ca);
e.Row.Cells[8].Controls.Add(cga);
e.Row.Cells[9].Controls.Add(cb);
...
}
A GridView has built-in sorting capabilities. Depending on the dataset you are using to populate the data, you likely don't need to manually handle anything manually, let alone with the ViewState.
Check out the second example on this MSDN page and note that it never does anything manually with the ViewState... the OnSorting and OnSorted events are there just to display extra information or to impose requirements:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx
If you post a bit more of your code (including your .aspx pages, the markup for the modal popups, and the code for the loadModals() function, we might be able to better help you.