Ok I have seen to many post about this and nothing work for me, I just do not get it. I have a simple datagrid which i want to add rows, NOTE: this datagrid have no relation with database so everything has to be done datatable or arraylist whatever you want to help me.
Im going to post a few lines of what i have so far.
<asp:DataGrid ID="dgHistoric" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnItemCommand="InsertSaveHist" >
<Columns>
<asp:TemplateColumn HeaderText="OSR">
<ItemTemplate>
<asp:Label ID="txtHisOsr" runat="server" Width="25" Text='<% #DataBinder.Eval(Container.DataItem, "Opsheetrev").ToString() %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFHisOsr" runat="server" Width="25"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Eng Dwg Rev.">
<ItemTemplate>
<asp:Label ID="txtHisEdr" runat="server" Width="25" Text='<% #DataBinder.Eval(Container.DataItem, "Engdwgrev").ToString() %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFHisEdr" runat="server" Width="25"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<FooterTemplate>
<asp:Button ID="btnAddHis" runat="server" Text="Add" CommandName="InsertHist" />
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
now in the load page function i have this because i want the first row blank.
DataTable dtOpe = new DataTable();
DataSet dsComp = new DataSet();
DataRow drOpe;
dtOpe.Columns.Add("Opsheetrev");
dtOpe.Columns.Add("Engdwgrev");
drOpe = dtOpe.NewRow();
drOpe[0] = "";
drOpe[1] = "";
dtOpe.Rows.Add(drOpe);
dsComp.Tables.Add(dtOpe);
dgHistoric.DataSource = dsComp;
dgHistoric.DataBind();
and finally the insert function
DataSet dsComp = new DataSet();
DataTable dt = (DataTable)dgHistoric.DataSource;
DataRow dr = dt.NewRow();
string osr = ((TextBox)e.Item.Cells[0].FindControl("txtFHisOsr")).Text;
string edr = ((TextBox)e.Item.Cells[1].FindControl("txtFHisEdr")).Text;
dr[0] = osr;
dr[1] = edr;
dt.Rows.Add(dr);
dsComp.Tables.Add(dt);
dgHistoric.DataSource = dt;
dgHistoric.DataBind();
an error is generated in this point DataRow dr = dt.NewRow(); saying NullReferenceException was unhandled by user code.
thanks in advance for any help.