Click here to Skip to main content
11,631,864 members (72,143 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 24-May-12 23:31pm
Edited 25-May-12 1:07am
RaisKazi33.6K
v3
Comments
Sandeep Mewara at 25-May-12 7:19am
   
Not clear. What do you mean by 'unbound grid'?
Pravinkarne.31 at 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
0 OriginalGriff 8,397
1 Sergey Alexandrovich Kryukov 8,009
2 Mika Wendelius 6,260
3 F-ES Sitecore 2,296
4 Suvendu Shekhar Giri 1,970


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 25 May 2012
Copyright © CodeProject, 1999-2015
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