Skip to main content
Email Password   helpLost your password?

Introduction

I was searching for a way to insert new rows into a DataGrid on a web page. There were lots of useful and useless links on the web, but neither of them was elegant enough. It is important to have an easy to understand and a good organized code. So here�s my solution for the problem.

Why use this solution

You don't need to create a separate dialog box (window) for adding an item to the table. Also, you can use the same logic (and code) for verifying the inserted data and the modified data (the data that�s about to be updated).

The solution

What I have done

  1. I've created a simple web project and added SqlConnection to it.
  2. Added a DataGrid and an insert button. I've also added an edit/update/cancel button column to the DataGrid.
  3. Created code for populating the DataGrid and implementing the insert.
    1. function for filling,
    2. function for inserting an empty row to the DataSource,
    3. function for binding the new table to the grid.
    4. and finally I have added the delegate for the DataGrid.OnItemCommand event.

Actually only two things are needed

  1. To handle the OnItemCommand event � here we write the execution code for the Insert, Update, Edit and Cancel commands.
  2. To do the inserting � we have to write the function for the button insert delegate � this piece of code is also responsible for changing the Update text to Insert (see the Edit/Update/Cancel button column).

The code

private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here

    if (! IsPostBack)
    {
        Fill();
        Bind();
    }
}

private void Fill()
{
    table = new DataTable();
    SqlDataAdapter adapter = 
      new SqlDataAdapter("select * from t_CPredvolba", conn);
    adapter.Fill(table);
}

private void Bind()
{
    grid.DataSource = table;
    grid.DataBind();
}

private void InsertEmpty()
{
    table.Rows.InsertAt(table.NewRow(), 0);
}

private void add_Click(object sender, System.EventArgs e)
{
    // inserting

    grid.EditItemIndex = 0;
    
    // modify text

    EditCommandColumn ecc = 
           (EditCommandColumn) grid.Columns[0];
    ecc.UpdateText = "Insert";
    
    // fill table, insert empty row, bind to datagrid

    Fill();
    InsertEmpty();
    Bind();
}

private void grid_ItemCommand(object source, 
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    // stop editing

    grid.EditItemIndex = -1;
    
    switch (e.CommandName)
    {
        case "Insert":
        break;
        
        case "Update":
        break;
        
        case "Cancel":
        EditCommandColumn ecc = 
                (EditCommandColumn) grid.Columns[0];
        ecc.UpdateText = "Update";
        break;
        
        case "Edit":
        // begin editing

        grid.EditItemIndex = e.Item.ItemIndex;
        break;
    }
    
    // fill and bind

    Fill();
    Bind();
}

Conclusion

I know it�s not much, but I hope it helps you out.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generaluse SqlDataReader? Pin
evanssthomas
3:42 27 Oct '09  
Generaltrapping the Insert Event Pin
Member 2553299
2:13 4 Jul '08  
GeneralCast Specified valid exception Pin
Member 2553299
1:02 3 Jul '08  
GeneralRe: Cast Specified valid exception Pin
Member 2553299
2:14 4 Jul '08  
Generalinsert into 2 tables at the same time Pin
lailah
23:43 7 Sep '07  
GeneralRe: insert into 2 tables at the same time Pin
balazs_hideghety
23:44 15 Sep '07  
Generalhow do you read the inserted cells ? Pin
ytb1000
23:28 20 Aug '07  
GeneralRe: how do you read the inserted cells ? Pin
balazs_hideghety
6:09 21 Aug '07  
GeneralIts working, but Pin
Elavarasu
2:58 19 Jun '06  
GeneralInsert vs Update Pin
DarKhan
10:26 28 Nov '05  
GeneralRe: Insert vs Update Pin
DarKhan
11:00 28 Nov '05  
GeneralRe: Insert vs Update Pin
abhiace
19:48 16 Mar '06  
GeneralRe: Insert vs Update Pin
Bruce E
7:52 6 Oct '06  
Generalto VB code Pin
Yang79
2:36 16 Sep '05  
GeneralRe: to VB code Pin
Anonymous
7:43 19 Sep '05  
GeneralChanging Font Pin
waycool66
12:05 15 Aug '05  
GeneralRe: Changing Font Pin
balazs_hideghety
2:16 16 Aug '05  
GeneralRe: Changing Font Pin
waycool66
4:44 16 Aug '05  
Generalchange font ? Pin
waycool66
12:03 15 Aug '05  
GeneralRe: change font ? Pin
Member 2553299
1:00 3 Jul '08  


Last Updated 14 Jul 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009