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

Adding a row to an ASP.NET DataGrid

By , 14 Jul 2005
 

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

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...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membernivasmaran17-Aug-10 0:22 
Questionuse SqlDataReader?memberevanssthomas27-Oct-09 2:42 
Generaltrapping the Insert EventmemberMember 25532994-Jul-08 1:13 
GeneralCast Specified valid exceptionmemberMember 25532993-Jul-08 0:02 
GeneralRe: Cast Specified valid exceptionmemberMember 25532994-Jul-08 1:14 
Generalinsert into 2 tables at the same timememberlailah7-Sep-07 22:43 
GeneralRe: insert into 2 tables at the same timememberbalazs_hideghety15-Sep-07 22:44 
Questionhow do you read the inserted cells ?memberytb100020-Aug-07 22:28 
AnswerRe: how do you read the inserted cells ?memberbalazs_hideghety21-Aug-07 5:09 
GeneralIts working, butmemberElavarasu19-Jun-06 1:58 
GeneralInsert vs UpdatememberDarKhan28-Nov-05 9:26 
GeneralRe: Insert vs UpdatememberDarKhan28-Nov-05 10:00 
GeneralRe: Insert vs Updatememberabhiace16-Mar-06 18:48 
GeneralRe: Insert vs UpdatememberBruce E6-Oct-06 6:52 
Generalto VB codememberYang7916-Sep-05 1:36 
GeneralRe: to VB codesussAnonymous19-Sep-05 6:43 
GeneralChanging Fontsusswaycool6615-Aug-05 11:05 
GeneralRe: Changing Fontmemberbalazs_hideghety16-Aug-05 1:16 
GeneralRe: Changing Fontsusswaycool6616-Aug-05 3:44 
Questionchange font ?susswaycool6615-Aug-05 11:03 
AnswerRe: change font ?memberMember 25532993-Jul-08 0:00 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 14 Jul 2005
Article Copyright 2005 by balazs_hideghety
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid