Click here to Skip to main content
15,885,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Solve this issue regarding dropdownlist and gridview. The scenario is as following: I have one dropdownlist and one gridview. Data from database is binding to dropdownlist. now dropdownlist has 1000+ employee names in DataTextField and employee emails in DataValueField. there is an ADD button with dropdownlist and when I add the selected item from dropdownlist it comes to gridview with autogenerated sr.no, empname(datatextfield) and email(datavaluefield) and one column of command field(DELETE Button) in the gridview. The issue is that when the selected item has been entered in the gridview from dropdownlist, it should be removed from the dropdownlist and when i delete such record from the gridview it should go back to dropdownlist. "Using 02 different Datatables for Dropdownlist and Gridview for binding data and viewstates as well."

THIS IS THE ADDBUTTON CODING ON WHICH THE DATA IS BEING INSERTED IN GRIDVIEW.

C#
protected void addemp_Click(object sender, EventArgs e)
{
        DataTable empemails = new DataTable();
        int srno = 1;
        empemails.Columns.Add("Sr.No");
        empemails.Columns.Add("EmpName");
        empemails.Columns.Add("Email");
        foreach (GridViewRow gvRow2 in GridView2.Rows)
        {
            empemails.Rows.Add();
            empemails.Rows[empemails.Rows.Count - 1][0] = gvRow2.Cells[0].Text; // empname
            empemails.Rows[empemails.Rows.Count - 1][1] = gvRow2.Cells[1].Text; // empname
            empemails.Rows[empemails.Rows.Count - 1][2] = gvRow2.Cells[2].Text; // emails
        }
        empemails.Rows.Add();
        empemails.Rows[empemails.Rows.Count - 1][0] = srno.ToString();
        empemails.Rows[empemails.Rows.Count - 1][2] = DropDownList2.SelectedItem;
        empemails.Rows[empemails.Rows.Count - 1][2] = DropDownList2.SelectedValue;
        ViewState["CurrentTable"] = empemails;
        GridView2.DataSource = ViewState["CurrentTable"] as DataTable;
        GridView2.DataBind();
    }
}


THIS IS THE CODE ON WHICH DROPDOWNLIST DATA IS BINDED FROM THE DATABASE

C#
protected void empemailbind()
{
    DataTable adp3 = new DataTable();
    OracleConnection dbConn = Conn.getConnection();
    string query3 = "select email,details from EmployeeTABLE";
    OracleCommand cmd3 = new OracleCommand(query3, dbConn);
    dbConn.Open();
    OracleDataAdapter dr3 = new OracleDataAdapter(cmd3);
    dr3.Fill(adp3);
    DropDownList2.DataSource = adp3;
    DropDownList2.DataTextField = "detail";
    DropDownList2.DataValueField = "email";
    DropDownList2.DataBind();
    dbConn.Close();
}


THIS IS THE CODE ON WHICH ROW_DELETING IS BEING PERFORMED IN THE GRIDVIEW.

C#
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    //LinkButton button = (LinkButton)GridView2.FindControl("(X)");
    DataTable dt = ViewState["CurrentTable"] as DataTable;
    //
    int rowIndex = Convert.ToInt32(e.RowIndex);
    if (dt.Rows.Count >= 0)
    {
        dt.Rows[rowIndex].Delete();
    }
    ViewState["CurrentTable"] = dt;
    GridView2.DataSource = ViewState["CurrentTable"] as DataTable;
    GridView2.DataBind();
}



help.. Thanks.
Posted
Updated 12-May-14 1:15am
v2
Comments
Thomas ktg 17-Sep-13 7:57am    
After adding the record to the gridview, delete the added record from the DropDownList. In the same way after deleting the record from the gridview, add the deleted record to the DropDownList. I dont think this is going to be difficult for you as you have already completed the ninety percentage work of your implementation.
AR547 17-Sep-13 8:03am    
If it was much easy then there must be a reason of posting that question here...
Thanks7872 17-Sep-13 7:57am    
1000+ employee names? in dropdownlist? Are you sure? Well,where is the question?
AR547 17-Sep-13 8:02am    
Yeah there are number of employees in it. Question is that I want to delete the employee name from the dropdownlist when it is added to the gridview. So the user cannot add ROHAN twice or thrice.... similarly when deleting from gridview .. e.g if i delete ROHAN from Gridview then it should go back to the dropdownlist... hope you got my question.
Thanks7872 17-Sep-13 8:05am    
Got your question but do you think some one would like to scroll through that many names to select e.g. 778th one?

1 solution

Hi,
Try this.
C#
protected void addemp_Click(object sender, EventArgs e)
{
      DataTable empemails = new DataTable();
        int srno = 1;
        empemails.Columns.Add("Sr.No");
        empemails.Columns.Add("EmpName");
        empemails.Columns.Add("Email");
        foreach (GridViewRow gvRow2 in GridView2.Rows)
        {
            empemails.Rows.Add();
            empemails.Rows[empemails.Rows.Count - 1][0] = gvRow2.Cells[0].Text; // empname
            empemails.Rows[empemails.Rows.Count - 1][1] = gvRow2.Cells[1].Text; // empname
            empemails.Rows[empemails.Rows.Count - 1][2] = gvRow2.Cells[2].Text; // emails
        }
        empemails.Rows.Add();
        empemails.Rows[empemails.Rows.Count - 1][0] = srno.ToString();
        empemails.Rows[empemails.Rows.Count - 1][1] = DropDownList2.SelectedItem;
        empemails.Rows[empemails.Rows.Count - 1][2] = DropDownList2.SelectedValue;
        ViewState["CurrentTable"] = empemails;
        GridView2.DataSource = ViewState["CurrentTable"] as DataTable;
        GridView2.DataBind();
        // add this
        DropDownList2.Items.RemoveAt(DropDownList2.SelectedIndex);
}

protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    //LinkButton button = (LinkButton)GridView2.FindControl("(X)");
    DataTable dt = ViewState["CurrentTable"] as DataTable;
    //
    int rowIndex = Convert.ToInt32(e.RowIndex);

    //Add this
    string text= Convert.ToString(dt.Rows[rowIndex][1]);
    string value = Convert.ToString(dt.Rows[rowIndex][2]);
    DropDownList2.Items.Add(text, value);


    if (dt.Rows.Count >= 0)
    {
        dt.Rows[rowIndex].Delete();
    }
    ViewState["CurrentTable"] = dt;
    GridView2.DataSource = ViewState["CurrentTable"] as DataTable;
    GridView2.DataBind();
}

Hope it helps you.
Thanks.
 
Share this answer
 
v2
Comments
AR547 17-Sep-13 8:14am    
Dear Harshil_Raval
Your Code is working but only in row deleting in gridview... if I add Harshil_Raval from dropdownlist it doesnot delete that name from dropdownlist and after clicking again on add button while selectin Harshil_Raval it is adding the other names randomly to the gridview.
Harshil_Raval 17-Sep-13 8:17am    
Hi, row deleting works. Then why
DropDownList2.Items.RemoveAt(DropDownList2.SelectedIndex);
is not removing value from dropdown!! It should work.
AR547 17-Sep-13 8:27am    
Bro...............THANKSSSSSSSS ALOT....its working now...I have created another Update Panel on DropDownList...Working Perfect....Salute to your skills...Thanks again :)
AR547 17-Sep-13 8:22am    
Sorry to mention... I have applied UPDATE PANEL from AJAX... so its not deleting the item at once..else it is working when another button fires a post back. Any idea on this issue? :(
Harshil_Raval 17-Sep-13 8:24am    
In that case, take DropdownList in updatepanel. It will solve your issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900