Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Team,

I want to Edit Unbound GridView - which will altimatly save changes to my DataBase.

I have below code which is working perfectly fine.

But Problem is for BU_Code,DM_Code and Emp_Role I want DropDownList in my UnBound GridView.

Please Help how I can do this with UnBound GridView.

private System.Data.DataTable Populate_GridView()
  {
      string qry = "SELECT     tbl_Emp_Master.Emp_ID, tbl_Emp_Master.Emp_Name, tbl_Emp_Master.Login_ID,  tbl_Emp_Master.BU_Code, tbl_Emp_Master.DM_Code, " +
                          " tbl_Emp_Master.Emp_Role, tbl_Emp_Role.[Role_Desc], tbl_BU.BU_Name, tbl_DM.DM_Name " +
                      " FROM         tbl_DM INNER JOIN " +
                          " tbl_BU ON tbl_DM.BU_Code = tbl_BU.BU_Code INNER JOIN " +
                          " tbl_Emp_Master INNER JOIN " +
                          " tbl_Emp_Role ON tbl_Emp_Master.Emp_Role = tbl_Emp_Role.Emp_Role ON tbl_DM.DM_Code = tbl_Emp_Master.DM_Code AND  " +
                          " tbl_BU.BU_Code = tbl_Emp_Master.BU_Code";
      SqlCon.Open();
      SqlDataAdapter adp = new SqlDataAdapter(qry, SqlCon);
      System.Data.DataTable dt = new System.Data.DataTable();
      System.Data.DataRow dr = dt.NewRow();
      dt.Rows.InsertAt(dr, 0);
      adp.Fill(dt);
      SqlCon.Close();
      return dt;
  }
  private void View_Table()
  {
      System.Data.DataTable dt = Populate_GridView();
      GridView1.DataSource = dt;
      GridView1.EditIndex = -1;
      GridView1.DataBind();
      ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
  }

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      SqlCommand cmd = new SqlCommand();
      cmd.CommandText = "UPDATE tbl_Emp_Master SET Emp_ID= '" + e.NewValues[0].ToString() + "', Emp_Name ='" + e.NewValues[1].ToString() + "', Login_ID='" + e.NewValues[2].ToString() + "', BU_Code='" + e.NewValues[3].ToString() + "', DM_Code='" + e.NewValues[4].ToString() + "', Emp_Role='" + e.NewValues[5].ToString() + "' " +
                       "WHERE  Emp_ID= '" + e.NewValues[0].ToString() + "'";
      cmd.Connection = SqlCon;
      SqlCon.Open();
      cmd.ExecuteNonQuery();
      SqlCon.Close();
      View_Table();

  }
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
      System.Data.DataTable dt = Populate_GridView();
      GridView1.DataSource = dt;
      GridView1.EditIndex = e.NewEditIndex; //Enable Edit Mode for Selected Row
      GridView1.DataBind();
      ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
  }
  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
      View_Table();
  }
Posted
Updated 25-May-12 1:07am
v3
Comments
Sandeep Mewara 25-May-12 7:19am    
Not clear. What do you mean by 'unbound grid'?
Pravinkarne.31 25-May-12 21:06pm    
I mean - I have one GridView with no columns, I am populating columns at Run time - by query.
Problem is - When I click on Edit Button on gridview all the columns are shwon in textbox , But for Some columns - BU_Code,DM_Code and Emp_Role I want to show DropDown List. So that User can select from dropdown list and then I can update the same into DataBase

1 solution

You could achieve it by using the templates within the Gridview control, an example of using a combobox in the detials view can been seen here

http://blogs.msdn.com/robburke/archive/2005/04/01/404727.aspx


http://www.floresense.com/resc_center/?art=1320


How to create custom bound fields in GridView

http://msdn.microsoft.com/en-us/library/ms228046.aspx
 
Share this answer
 
v2

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