Tuesday, 10 September 2013

Synchronyzing data between dropdownlist and gridview using datatable and viewstate

Synchronyzing data between dropdownlist and gridview using datatable and
viewstate

NEW TO STACKOVERFLOW AND POSTING MY 1st QUESTION. 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.
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
protected void empemailbind()
{
DataTable adp2 = new DataTable();
OracleConnection dbConn = Conn.getConnection();
string query3 = "select email,details from ..... so on";
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.
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.

No comments:

Post a Comment