Click here to Skip to main content
Click here to Skip to main content

Adding a row to an ASP.NET DataGrid

, 14 Jul 2005
Rate this:
Please Sign up or sign in to vote.
This article describes an easy and elegant way of inserting a row into a DataGrid.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

balazs_hideghety
Web Developer
Slovakia Slovakia
Since 1999 I work in IT. Worked 2-3 yrs with Borland Builder C++. Since .NET appeared, I program in C#, ASP.NET.
 
You may know the current technologies, but still there's a lot of experience to gain. IT's evolving all the time.
 
From 2006 I'm a MCP. Now I'm focusing on technologies like: NHibernate, NSpring...

Comments and Discussions

 
GeneralMy vote of 1 Pinmembernivasmaran17-Aug-10 1:22 
Questionuse SqlDataReader? Pinmemberevanssthomas27-Oct-09 3:42 
Generaltrapping the Insert Event PinmemberMember 25532994-Jul-08 2:13 
GeneralCast Specified valid exception PinmemberMember 25532993-Jul-08 1:02 
GeneralRe: Cast Specified valid exception PinmemberMember 25532994-Jul-08 2:14 
Generalinsert into 2 tables at the same time Pinmemberlailah7-Sep-07 23:43 
GeneralRe: insert into 2 tables at the same time Pinmemberbalazs_hideghety15-Sep-07 23:44 
Questionhow do you read the inserted cells ? Pinmemberytb100020-Aug-07 23:28 
AnswerRe: how do you read the inserted cells ? Pinmemberbalazs_hideghety21-Aug-07 6:09 
GeneralIts working, but PinmemberElavarasu19-Jun-06 2:58 
GeneralInsert vs Update PinmemberDarKhan28-Nov-05 10:26 
GeneralRe: Insert vs Update PinmemberDarKhan28-Nov-05 11:00 
GeneralRe: Insert vs Update Pinmemberabhiace16-Mar-06 19:48 
GeneralRe: Insert vs Update PinmemberBruce E6-Oct-06 7:52 
Generalto VB code PinmemberYang7916-Sep-05 2:36 
GeneralRe: to VB code PinsussAnonymous19-Sep-05 7:43 
GeneralChanging Font Pinsusswaycool6615-Aug-05 12:05 
GeneralRe: Changing Font Pinmemberbalazs_hideghety16-Aug-05 2:16 
GeneralRe: Changing Font Pinsusswaycool6616-Aug-05 4:44 
Questionchange font ? Pinsusswaycool6615-Aug-05 12:03 
AnswerRe: change font ? PinmemberMember 25532993-Jul-08 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 14 Jul 2005
Article Copyright 2005 by balazs_hideghety
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid