AsishRaj
asked on
Insert Row in Datagrid
i have the below code.
Now i want to add a row when Add button is clicked.
How can i achieve that
Now i want to add a row when Add button is clicked.
How can i achieve that
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Generate rows and cells.
Dim dt As New DataTable()
dt.Rows.Add(dt.NewRow())
dgTest.DataSource = dt
dgTest.DataBind()
End If
End Sub
Protected Sub dgTest_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemCreated
If e.Item.ItemType = ListItemType.EditItem Then
Dim e1 As TextBox = DirectCast(e.Item.FindControl("TextBox3"), TextBox)
e1.Width = Unit.Parse("75px")
e1 = DirectCast(e.Item.FindControl("TextBox2"), TextBox)
e1.Width = Unit.Parse("75px")
e1 = DirectCast(e.Item.FindControl("TextBox1"), TextBox)
e1.Width = Unit.Parse("75px")
End If
End Sub
Sub doEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dgTest.ShowFooter = False
dgTest.EditItemIndex = e.Item.ItemIndex
BindData()
End Sub
Sub doCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dgTest.ShowFooter = True
dgTest.EditItemIndex = -1
BindData()
End Sub
Sub doInsert(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'add a new row to datagrid
End Sub
aspx
<asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false" BorderColor="White" BorderStyle="Solid" BorderWidth="2px"
CellPadding="3" CellSpacing="1" Font-Names="Tahoma" Font-Size="Small"
HorizontalAlign="Center" ShowFooter="True"
OnEditCommand ="doEdit" OnCancelCommand="doCancel" >
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<Columns>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Shop"
DataTextField="ShopName" DataValueField="ShopCode">
</asp:DropDownList>
<asp:SqlDataSource ID="Shop" runat="server"
ConnectionString="<%$ ConnectionStrings:eTFSConnectionString %>"
SelectCommand="SELECT * FROM [DEFShops]"></asp:SqlDataSource>
</EditItemTemplate>
<HeaderTemplate>
Shop Name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
Docket Number
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
Docket Total
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
Docket Date
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn
ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"
EditText="Edit" HeaderText="Edit">
</asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="Delete">
<FooterTemplate>
<asp:Button CommandName="Insert" Text="Add" ID="btnAdd" Runat="server" OnClick="AddRow" />
</FooterTemplate>
<ItemTemplate>
<asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<HeaderStyle BackColor="#3366CC" Font-Bold="False" Font-Italic="False"
Font-Names="Tahoma" Font-Overline="False" Font-Size="Small"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
ForeColor="White" />
</asp:DataGrid>
ASKER
its one...
I only want to add a new row to the datagrid. This to me sounds a bit challenging but not impossible.
>>3. If it is the former, you should know that this data only gets rendered on the browser and does not get rendered anywhere else.
Ok I couldnt get what you meant here
I only want to add a new row to the datagrid. This to me sounds a bit challenging but not impossible.
>>3. If it is the former, you should know that this data only gets rendered on the browser and does not get rendered anywhere else.
Ok I couldnt get what you meant here
Ashish,
All this means is that the data the user enters on the screen does not get saved anywhere. So the next time the user logs on to this site, it shows up only with the grid's headers and nothing else. This is because the application did not save the data ('persisted the data' to speak technical) so it could be pulled and displayed to the user at a later date.
I hope I've explained it so you can get a better picture of what path you can take.
Arun
All this means is that the data the user enters on the screen does not get saved anywhere. So the next time the user logs on to this site, it shows up only with the grid's headers and nothing else. This is because the application did not save the data ('persisted the data' to speak technical) so it could be pulled and displayed to the user at a later date.
I hope I've explained it so you can get a better picture of what path you can take.
Arun
ASKER
OK, i understood.
This is what i am trying to achieve.
I have some selection criteria when a user wants to register.
I have an empty datagrid with the headers.
when the user clicks add - names will be changed to meaning full names- a new row will be created in the datagrid, By default that will be in edit mode where they will select from the Dropdownlist, textbox etc, defined in the datagrid. when the click add again then new row will be added and again they will get to enter the details.
When they click Finish - all the raw data will get processed and then they will be redirected to the neccessary page.
Thing to note:
Raw data are meaningless therefore there is no need to keep them in the table - but once they get processed - they are updated in the tables. this is sort of the business rule created
This is the UI requirement - the user needs to see the previous details entered unitl he clicks finish, also give the ability to edit and delete those row data
This is the whole reason i am trying to insert a new row in the datagrid
This is what i am trying to achieve.
I have some selection criteria when a user wants to register.
I have an empty datagrid with the headers.
when the user clicks add - names will be changed to meaning full names- a new row will be created in the datagrid, By default that will be in edit mode where they will select from the Dropdownlist, textbox etc, defined in the datagrid. when the click add again then new row will be added and again they will get to enter the details.
When they click Finish - all the raw data will get processed and then they will be redirected to the neccessary page.
Thing to note:
Raw data are meaningless therefore there is no need to keep them in the table - but once they get processed - they are updated in the tables. this is sort of the business rule created
This is the UI requirement - the user needs to see the previous details entered unitl he clicks finish, also give the ability to edit and delete those row data
This is the whole reason i am trying to insert a new row in the datagrid
Try this line of code behind the add new button
dt.Rows.Add(dt.NewRow())
dgtest.databind()
dt.Rows.Add(dt.NewRow())
dgtest.databind()
CodeCruiser bhai.. he's done that already.
Asish, please go through the tutorials for a gridview. I'm sure you'll get a better understanding of how to do things:
http://www.codersource.net/asp_net_grid_view_whidbey.aspx
http://www.devx.com/dotnet/Article/22141
Asish, please go through the tutorials for a gridview. I'm sure you'll get a better understanding of how to do things:
http://www.codersource.net/asp_net_grid_view_whidbey.aspx
http://www.devx.com/dotnet/Article/22141
Yaar lambi lambi explanations likhi thain maine soocha seedha saadha jawab doon.
LOL. Good one CodeCruiser.
ASKER
Yaar aapka seedha saadha jawab toh kaam nahi karega. toh aap kyon nahi seedhe seedhe apne try karke tab batao.
I have already have a work around which i am using but since i got involved in this i think its good to solve it so that it can be usefull for some in future who is stuck with the same problem.
I have already have a work around which i am using but since i got involved in this i think its good to solve it so that it can be usefull for some in future who is stuck with the same problem.
Ashish.. dude chill. Were you able to get your hands around the tutorial?
ASKER
Sorry CodeCruiser, ulta baat ke ulta response diya.
Yeap i have had a peek at those tutorial.
I think the bottom line is that the datagrid is empty, when we try to add a new row it adds it at row one.
So a workaround could be when add button is clicked, we retrieve the data store it in the dataset or datatable and then we add a new row.
I havent tried that, but do think it will work.
So far i was trying to add to datagrid without retriveing the details. Silly of me
Yeap i have had a peek at those tutorial.
I think the bottom line is that the datagrid is empty, when we try to add a new row it adds it at row one.
So a workaround could be when add button is clicked, we retrieve the data store it in the dataset or datatable and then we add a new row.
I havent tried that, but do think it will work.
So far i was trying to add to datagrid without retriveing the details. Silly of me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help Arun.
this article explains it in dept. got the link from your above post.
http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx
this article explains it in dept. got the link from your above post.
http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx
good that things worked out for you.
Before we move on with this application, here are some of the things you need to know/answer:
1. When you say 'add a row to the datagrid', do you mean, just add it to the datagrid or do you want that data to be added to a database and then attach the data in the database to the grid?
2. If it is the latter, you need to come up with a database design (with table structures and views and stored procedures as needed). You also need to know what database you would want to use (SQL Server, MS Access, Oracle..)
3. If it is the former, you should know that this data only gets rendered on the browser and does not get rendered anywhere else.
Let me know which way you are going.