Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:13:39+00:00 2026-06-10T17:13:39+00:00

When adding items to a gridview, I want the user to be able to

  • 0

When adding items to a gridview, I want the user to be able to select and edit/remove the item before writing the file (from the gridview entries) to a csv format. When I click on “edit” and update the information, I get an error. There is no row at position 0. (or whatever row you select to edit). Here is my code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SetFocus(parttxtbox);
        table = new DataTable();
        MakeDataTable();
    }
    else
        table = (DataTable)ViewState["table"];

    ViewState["table"] = table;
}

protected void MakeDataTable()
{
    table.Columns.Add("Part", typeof(string));
    table.Columns.Add("Quantity", typeof(Int32));
    table.Columns.Add("Ship-To", typeof(string));
    table.Columns.Add("Requested Date", typeof(string));
    table.Columns.Add("Shipping Method", typeof(string));

    //Persist the table in the Session object.
    Session["table"] = table;

    //Bind data to the GridView control.
    BindData();
}

protected void addbtn_Click(object sender, EventArgs e)
{
    part = parttxtbox.Text.ToUpper();
    shipto = shiptotxtbox.Text;
    reqdate = reqdatecal.SelectedDate.ToShortDateString();
    shipmthd = shipddl.SelectedItem.ToString();
    CreateTable();
 }

public void CreateTable()
{
    DataRow row = table.NewRow();
    row["Part"] = part;
    row["Quantity"] = qty;
    row["Ship-To"] = shipto;
    row["Requested Date"] = reqdate;
    row["Shipping Method"] = shipmthd;
    table.Rows.Add(row);

    griditems.DataSource = table.DefaultView;
    griditems.DataBind();
}

private void BindData()
{
    griditems.DataSource = table;
    griditems.DataBind();
}

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["table"];

    //Update the values.           
    GridViewRow row = griditems.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString();


    //Reset the edit index.
    griditems.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();

    ////Somewhat works, doesn't update the part field with the text entered
    //TextBox partedit = (TextBox)griditems.Rows[e.RowIndex].FindControl("Part");
    //DataRow row = table.NewRow();
    //row["Part"] = partedit;
    //row["Quantity"] = qty;
    //row["Ship-To"] = shipto;
    //row["Requested Date"] = reqdate;
    //row["Shipping Method"] = shipmthd;
    //table.Rows.Add(row);
}

The commented out part that says “Somewhat works” will write blanks in the field updated when you click update.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:13:40+00:00Added an answer on June 10, 2026 at 5:13 pm

    The time you are adding data table in Session there is no row in it, thats why you get error "There is no row at position 0."

    You are assigning table from viewstate to table it would be session.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetFocus(parttxtbox);
            table = new DataTable();
            MakeDataTable();
        }
        else
        if(Session["table"] != null)
            table = (DataTable)ViewState["table"];
    }
    
    protected void MakeDataTable()
    {
        table.Columns.Add("Part", typeof(string));
        table.Columns.Add("Quantity", typeof(Int32));
        table.Columns.Add("Ship-To", typeof(string));
        table.Columns.Add("Requested Date", typeof(string));
        table.Columns.Add("Shipping Method", typeof(string));
    
        //Persist the table in the Session object.
        Session["table"] = table; //This adds table without any record in it. 
    
        //Bind data to the GridView control.
        BindData();
    }
    

    Add data table to session after you add records in datatable.

    protected void addbtn_Click(object sender, EventArgs e)
    {
        part = parttxtbox.Text.ToUpper();
        shipto = shiptotxtbox.Text;
        reqdate = reqdatecal.SelectedDate.ToShortDateString();
        shipmthd = shipddl.SelectedItem.ToString();
        CreateTable();
        Session["table"] = table;
     }
    

    Update the Session[“table”] in griditems_RowUpdating after updating.

    protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["table"];
    
        //Update the values.           
        GridViewRow row = griditems.Rows[e.RowIndex];
        dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString();
    
    
        //Reset the edit index.
        griditems.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    
        Session["table"] = dt; 
    }
    

    Note: The way you are trying to get the updating recording is not appropriate you should read more about how to access data on row updating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding items dynamically when a row is selected from GridView. 1. How can
I'd like to move items from one list view to another. adding them to
After adding <item>170,000</items> to string.xml . It becomes so slow in building workspace, I
I am having a difficulty adding an item to my listbox. I want to
How do you setup two way binding when removing/adding items from a list data
I have an item template in a GridView and I want to create a
I'm testing adding items to the shopping cart like this: $item = $this->model->getSingleItem(); for($i
Basically I'm dynamically adding items to a listbox. Inside of each listbox item I
Please i need some help in adding items to a JComboBox in Java from
I am adding items to a ListBox like so: myListBox.Items.addRange(myObjectArray); and I also want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.