I have a gridview that has a blank row with 2 input fields: file to upload, and a description of the file. They click Add and a new row is added, Remove and the row is removed.
The problem is:
When a new row is added, I loop through the existing gridview rows, store the data in a dataset, and rebind. In debug mode, I see the values I entered but when I rebind, it's not displayed in the gridview.
Here's the code aspx code:
[code=html]
<asp:GridView ID="gFiles" runat="server" AutoGenerateCol umns="False" CellPadding="4" ForeColor="#333 333" GridLines="None " Visible="False" >
<RowStyle BackColor="#F7F 6F3" ForeColor="#333 333" />
<Columns>
<asp:TemplateFi eld HeaderText="Fil e to upload">
<ItemTemplate >
<asp:FileUplo ad ID="FileToUploa d" runat="server" Width="400px" />
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription">
<ItemTemplate >
<asp:TextBox ID="tbDesc" runat="server" Height="24px"
MaxLength="200" TextMode="Multi Line" Width="300px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld ShowHeader="Fal se" HeaderText="Add ">
<ItemTemplate >
<asp:LinkButt on ID="btnAdd" runat="server" CausesValidatio n="false" CommandName="Ad d"
Text="Add"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True" DeleteText="Rem ove" /></Columns>
<HeaderStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" HorizontalAlign ="Left" />
<AlternatingRow Style BackColor="Whit e" ForeColor="#284 775" />
</asp:GridView>
[/code]
Here's the code behind:
[code=vbnet]
If dtFiles.Rows.Co unt = 0 Then
'crate the blank table
dtFiles.Columns .Add("FileToUpl oad", GetType(FileUpl oad))
dtFiles.Columns .Add("tbDesc", GetType(TextBox ))
dtFiles.Columns .Add("Add", GetType(LinkBut ton))
End If
'Convert datagrid to dtFiles
Dim gr As GridViewRow
Dim nr As DataRow
For Each gr In gFiles.Rows
nr = dtFiles.NewRow
Dim tb As New TextBox
tb = CType(gr.FindCo ntrol("tbDesc") , TextBox)
nr(1) = tb
dtFiles.Rows.Ad d(nr)
Next
If e.CommandName = "Add" Then
'Add new row to dtFiles
nr = dtFiles.NewRow
dtFiles.Rows.Ad d(nr)
ElseIf e.CommandName = "Remove" Then
dtFiles.Rows.Re moveAt(gFiles.S electedIndex)
gFiles.DataSour ce = dtFiles
gFiles.DataBind ()
End If
gFiles.DataSour ce = dtFiles
gFiles.DataBind ()[/code]
The problem is:
When a new row is added, I loop through the existing gridview rows, store the data in a dataset, and rebind. In debug mode, I see the values I entered but when I rebind, it's not displayed in the gridview.
Here's the code aspx code:
[code=html]
<asp:GridView ID="gFiles" runat="server" AutoGenerateCol umns="False" CellPadding="4" ForeColor="#333 333" GridLines="None " Visible="False" >
<RowStyle BackColor="#F7F 6F3" ForeColor="#333 333" />
<Columns>
<asp:TemplateFi eld HeaderText="Fil e to upload">
<ItemTemplate >
<asp:FileUplo ad ID="FileToUploa d" runat="server" Width="400px" />
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription">
<ItemTemplate >
<asp:TextBox ID="tbDesc" runat="server" Height="24px"
MaxLength="200" TextMode="Multi Line" Width="300px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld ShowHeader="Fal se" HeaderText="Add ">
<ItemTemplate >
<asp:LinkButt on ID="btnAdd" runat="server" CausesValidatio n="false" CommandName="Ad d"
Text="Add"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True" DeleteText="Rem ove" /></Columns>
<HeaderStyle BackColor="#5D7 B9D" Font-Bold="True" ForeColor="Whit e" HorizontalAlign ="Left" />
<AlternatingRow Style BackColor="Whit e" ForeColor="#284 775" />
</asp:GridView>
[/code]
Here's the code behind:
[code=vbnet]
If dtFiles.Rows.Co unt = 0 Then
'crate the blank table
dtFiles.Columns .Add("FileToUpl oad", GetType(FileUpl oad))
dtFiles.Columns .Add("tbDesc", GetType(TextBox ))
dtFiles.Columns .Add("Add", GetType(LinkBut ton))
End If
'Convert datagrid to dtFiles
Dim gr As GridViewRow
Dim nr As DataRow
For Each gr In gFiles.Rows
nr = dtFiles.NewRow
Dim tb As New TextBox
tb = CType(gr.FindCo ntrol("tbDesc") , TextBox)
nr(1) = tb
dtFiles.Rows.Ad d(nr)
Next
If e.CommandName = "Add" Then
'Add new row to dtFiles
nr = dtFiles.NewRow
dtFiles.Rows.Ad d(nr)
ElseIf e.CommandName = "Remove" Then
dtFiles.Rows.Re moveAt(gFiles.S electedIndex)
gFiles.DataSour ce = dtFiles
gFiles.DataBind ()
End If
gFiles.DataSour ce = dtFiles
gFiles.DataBind ()[/code]
Comment