Deleting a row when the user presses delete button - c#

This might be a easy problem but i don't see what i'm doing wrong here. The code is:
protected void dgvSelected_RowCommand (object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="Delete")
{
if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
{
int RowIndex = Convert.ToInt32(e.CommandArgument);
dgvSelected.DeleteRow(RowIndex);
dgvSelected.DataBind();
}
}
}
And at the end of the class i have this method.
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I'm using asp .net and i have a gridview that several rows and at each row i have a delete button where the user can press it to delete the row. For the moment when i press the delete button the whole gridview disappears. What am i doing wrong?

Try this
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dgvSelected.DataBind();
}

Since the gridview data was from antoher gridview, the solution was to make a viewstate variable and save the datatable in it. And then to delete one row:
protected void dgvSelected_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["SelectedValue"] as DataTable;
dt.Rows[index].Delete();
ViewState["SelectedValue"] = dt;
dgvSelected.DataSource = dt;
dgvSelected.DataBind();
}

Related

How to display some column in grid view?

I want display specific column in data grid view for profile of user after log in the system but my problem it is display all columns in C# with asp.net sql server, any one can help me?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["MyID"] != null)
{
if (!IsPostBack)
{
string Id1 = Request.QueryString["MyID"];
dt = d.userunfo(Id1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
Do you want this like?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
You can check this answer.
GridView Hide Column by code
Make sure you have specified the Datasource property of column as database column name
Add OnRowDataBound of your GridView
<asp:GridView ID="GridViewID" runat="server" OnRowDataBound="gvTest_RowDataBound" >
in RowDataBound
First, check the roles of user and then you can check the condition and hide your column
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check User Type
int userType = //Your User Type Here;
if (userType == 1)
{
GridViewID.Columns[15].Visible = false;
}
else if (userType == 2 || userType == 3)
{
GridViewID.Columns[5].Visible = false;
GridViewID.Columns[6].Visible = false;
}
}

Gridview row updating button not working

My gridview row updating button is not working when I press edit and then press on the button to apply the changes it doesn't do anything. Am I missing something?
My code looks like this
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string afspraak = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditAfspraak"))).Text;
string uitleg = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditUitleg"))).Text;
int id = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditID"))).Text);
_controller.RecordUpdatenTblAfspraken(afspraak, uitleg, id);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
ASP.NET
Couldn't paste it correctly so it's here:
https://pastebin.com/S3S09nLL
Highlighted button isn't working
You have to rebind the grid after you change the EditIndex.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
The same goes for GridView1_RowCancelingEdit and when update has succeeded in GridView1_RowUpdating. This is assuming you added the OnRowEditing, OnRowCancelingEdit and OnRowUpdating events to the GridView.
Also as user7415073 commented, the binding of the GridView must be inside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = mijnDatabaseBron;
GridView1.DataBind();
}
}
And see my answer here as to how you can easily pass the EditID to code behind.
Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#

Detailsview on ModalPopUp only displays first row of gridview

I have a detailsview inside a modalpopup that is supposed to be displaying the selected row in my gridivew, but it is only displaying the first row in my gridview. What am I doing incorrectly? Thank you for your help.
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
this.dvstudent.Visible = true;
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
protected void Selecttomodifystudent_Click(object sender, EventArgs e)
{
LinkButton buttonstudent = sender as LinkButton;
GridViewRow row = (GridViewRow)buttonstudent.NamingContainer;
this.sqlModifyStudent1.SelectParameters.Clear();
this.dvstudent.DataBind();
this.UpdatePanelDetailsView1.Update();
this.modalstudent.Show();
}
I found the answer to my problem. It was right before my eyes:
protected void gvStudent_SelectedIndexChanged(object sender, EventArgs e)
{
dvstudent.PageIndex = gvStudent.SelectedIndex;
}

Gridview won't rebind data after changing Gridview page

I am trying to populate my gridview with amazon search results. At the moment when the page loads, datasource is filled with data. What I am trying to do is show the data after pressing the Search button, but it displays "No Records Found". I have tried many different ways, the only way it worked, was without postback, but then the issue was that every time I changed the page on the gridview, GetProducts("Playstation") command was initiated over again.
The solution I have been searcing for: Load the page -> click button -> fill the gridview with data -> When choosing new page in gridview, data is displayed but Getproducts("Playstation") is not initiated again.
Is there a way to do this ?
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(this.GreetingBtn_Click);
if (!Page.IsPostBack) {
AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
GridView1.DataSource = us.GetProducts("Playstation");
}
}
void Search(Object sender, EventArgs e) {
Button clickedButton = (Button) sender;
GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
EDIT
I figured it out thanks to FastGeeks anwser. I added variable ds to the code. and made the following changes:
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e) {
Button1.Click += new EventHandler(this.GreetingBtn_Click);
}
void Search(Object sender, EventArgs e) {
Button clickedButton = (Button) sender;
AmazonSearch us = new Amazon.PAAPI.AmazonSearch();
ds.Tables.Add(us.GetProducts("Playstation"));
GridView1.DataSource = ds;
Session["ds"] = ds;
GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
GridView1.PageIndex = e.NewPageIndex;
ds = (DataSet)Session["ds"];
GridView1.DataSource = ds;
GridView1.DataBind();;
}
You need to assign DataSource again in PageIndexChanging before binding it.
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = us.GetProducts("Playstation");
GridView1.DataBind();
}
Similarly assign DataSource in search method too.
void Search(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
GridView1.DataSource = us.GetProducts("Playstation");
GridView1.DataBind();
}
My take on this would be to store the results from the Amazon search into a DataTable, and then store the DataTable in a session variable; this is entirely possible because a DataTable is Serializable and will store in the session.
Then in your grid_PageIndexChanging event, and Search method you can retrieve the DataTable from session and re-assign your data without repeating the Amazon search.

Persisting DataTable or GridView DataSource

I have a Click Event that fills a DataTable and the DataTable is the source of my GridView.
Then I have another click event that tries to get the GridView DataSource e converts it back to a DataTable Like:
DataTable dt = (DataTable)GridView1.DataSource;
But the Datasource returns null. Event if I put the code and the Page_Init event waiting for the right postBack
so I would like to know how can i persist the datasource of the gridview, or the DataTable
edited as required:
here is the whole code:
ps: the Page_Init was another try to get the datasource
private DataTable _dataTable;
public DataTable dataTable
{
get { return _dataTable; }
set { _dataTable = value; }
}
protected void Page_Init(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
string ctrlname = BLL.Common.GetPostBackControlId(this.Page);
if(ctrlname == "ButtonDownload")
{
DataTable dt = (DataTable)GridView1.DataSource;
}
}
}
protected void Filter_Click(object sender, EventArgs e)
{
string[] status = new string[2];
status[0] = "Paga";
status[1] = "Disponivél";
dataTable = BLL.PagSeguro.GetTransactions(TextBoxInicio.Text, TextBoxFim.Text, status);
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
protected void GetDataSource(object sender, EventArgs e)
{
DataTable dt = (DataTable)GridView1.DataSource;
}
This might work for you.
public partial class Demo : System.Web.UI.Page
{
private DataTable _myData = null;
protected DataTable MyData
{
get
{
if (null == _myData)
{
// You would load your data here.
_myData = new DataTable();
}
return _myData;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Lets say you set your data source here
myGrid.DataSource = this.MyData;
}
protected void Rendering(object sender, EventArgs e)
{
// This is some other event that also needs to get at the data.
DataTable mydata = this.MyData;
}
protected void Unload(object sender, EventArgs e)
{
if (null != _myData)
{
_myData.Dispose();
_myData = null;
}
}
I'm pretty sure you can only access the datasource that way through the DataBound event or ItemDataBound event. You might be able to access the DataRowView for each item in the Items collection, but I'm not sure:
DataRow row = ((DataRowView)GridView1.Rows[0].DataItem).Row;
As for persisting the datasource, you need to consider whether that's a good idea. Your options for storing the datasource are Session or Cache, but if the result set is fairly small it might be more efficient to make another round trip when you need the datasource. Whatever you decide to do, don't store it in ViewState.

Categories