Click here to Skip to main content
12,072,809 members (77,924 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
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 25-May-12 0:31am
Edited 25-May-12 2:07am
RaisKazi34.1K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 25 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100