From your code i recon that you are working with web forms (i.e. you have a page object)
A possibility here is to use viewstate
Understanding ASP.NET View State[
^]
That beign said a gridview doesn't as such have controls, so what very likely happens is that you have a gridview which is wired up with some edit functionality in that case you need to catch those data as they get posted and well ... what? make a new row or save and updated? I guess you need this tutorial too:
Tutorial 12: Using TemplateFields in the GridView Control[
^]
So well the short of the long is that when you post something on a asp.net web.page you'll have access to the values in the Request.Form[] array and depending on if it is a client side or a server side control, after page load you could have access to it on the server side instance of a control.
UPDATE: OK, here's a solution to a similar problem using ViewState to persist the data and not using an ObjectDatasource which is propably what you want to do ultimately but sticking to the case at hand and keeping things as simple as possible with a grid :D This one will make a new grid on the initial GET and allow you to play with that as long as you keep posting by clicking the action linkbuttons on the grid.
The form:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AnittyGriddy.aspx.cs" Inherits="WebFormsProj.AnittyGriddy" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap.min.css" type="text/css" rel="stylesheet" />
<script src="Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="row">
<div class="col-xs-6"><label>Project scope</label></div>
<div class="col-xs-6">
<asp:DropDownList ID="ProjectDD" runat="server" Width="100%" OnSelectedIndexChanged="ProjectDD_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="All" Value="all" Selected="True" />
<asp:ListItem Text="Add" Value="add" />
</asp:DropDownList>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<asp:GridView ID="TheGrid" runat="server" Width="100%" Height="100%" AutoGenerateColumns="false" DataKeyNames="ProjectName" OnRowEditing="TheGrid_RowEditing" OnRowUpdating="TheGrid_RowUpdating" AutoGenerateEditButton="true" OnRowCancelingEdit="TheGrid_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Project" SortExpression="ProjectName">
<EditItemTemplate>
<asp:TextBox ID="ProjectNameText" runat="server" Text='<%#Bind("ProjectName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ProjectNameLabel" runat="server" Text='<%#Bind("ProjectName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Estimate" SortExpression="ProjectEstiamte">
<EditItemTemplate>
<asp:TextBox ID="ProjectEstimateText" runat="server" Text='<%#Bind("ProjectEstimate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ProjectEstimateLabel" runat="server" Text='<%#Bind("ProjectEstimate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
And the code behind:
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
namespace WebFormsProj
{
public partial class AnittyGriddy : System.Web.UI.Page
{
protected DataTable GridSource {
get { return (DataTable)ViewState["gridsource"]; }
set { ViewState["gridsource"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ConstructInitialGridSource();
}
BindGrid();
}
private void ConstructInitialGridSource()
{
var table = new DataTable();
table.Columns.Add(new DataColumn("ProjectName", typeof(string)));
table.Columns.Add(new DataColumn("ProjectEstimate", typeof(int)));
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
var row = table.NewRow();
row[0] = "Alfa";
row[1] = 100;
table.Rows.Add(row);
GridSource = table;
}
private void BindGrid()
{
TheGrid.DataSource = GridSource;
TheGrid.DataBind();
}
protected void ProjectDD_SelectedIndexChanged(object sender, EventArgs e)
{
var dropdown = (DropDownList)sender;
var todisplayordo = dropdown.SelectedValue;
switch (todisplayordo)
{
case "add":
{
var mytable = GridSource;
var thenew = mytable.NewRow();
thenew[0] = DateTime.Now.Ticks.ToString();
thenew[1] = 666;
mytable.Rows.Add(thenew);
GridSource = mytable;
TheGrid.EditIndex = mytable.Rows.Count - 1;
BindGrid();
break;
}
default:
{
break;
}
}
}
protected void TheGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
TheGrid.EditIndex = e.NewEditIndex;
TheGrid.DataBind();
}
protected void TheGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string keyField = e.Keys[0].ToString();
var mytable = GridSource;
var editRow = mytable.Rows.Find(e.Keys[0]);
var keys = Request.Form.AllKeys;
foreach (DataColumn clmn in mytable.Columns)
{
var idofclmn = keys.First(k => k.EndsWith(clmn.ColumnName + "Text"));
editRow[clmn.ColumnName] = Request.Form[idofclmn];
}
GridSource = mytable;
TheGrid.EditIndex = -1;
BindGrid();
}
protected void TheGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
TheGrid.EditIndex = -1;
TheGrid.DataBind();
}
}
}