Introduction This article explains how to make all the columns in the datagrid editable at the runtime. We can use the following methodology when we exactly don't know how many columns are going to be in the datatable.
Creating Datatable 1. Creating the datatable and binding it into the DataGrid
Dim myTextBox As TextBox // in page load give create a datatable and bind to it Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Calling the Function CreateTable Call CreateTable() End Sub Private Sub CreateTable() ' Creating the datatable with two columns Dim dtSample As New DataTable("Sample") Dim dr As DataRow dtSample.Columns.Add(New DataColumn("Name")) dtSample.Columns.Add(New DataColumn("Age")) 'adding sample rows into the datatable dr = dtSample.NewRow() dr("Name") = "First Entry" dr("Age") = 22 dtSample.Rows.Add(dr) dr = dtSample.NewRow() dr("Name") = "Second Entry" dr("Age") = 26 dtSample.Rows.Add(dr) dr = dtSample.NewRow() dr("Name") = "Third Entry" dr("Age") = 32 dtSample.Rows.Add(dr) ' Binding it to the datagrid Session("Sample") = dtSample DataGrid1.DataSource = dtSample DataGrid1.DataBind() End Sub
Loading TextBoxes 2.We have to dynamically load the TextBox into the datagrid columns in the ItemDataBoundEvent. We put the following piece of code to do that.
Dim liCounter As Integer If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim dt As DataTable dt = Session("Sample") For liCounter = 0 To dt.Columns.Count - 1 myTextBox = New TextBox myTextBox.Text = dt.Rows(e.Item.DataSetIndex)(liCounter) e.Item.Cells(liCounter).Controls.Add(myTextBox) Next End If
Now the Datagrid will all become the editable one with the textbox. Then the next step?. How to fetch the value from the datagrid.
Fetching Values 3). Let's place a button in the page , on Button Click event we are fetching the Values. Place the following piece of code in the Button Click Event.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dt As DataTable Dim liCounter As Short Dim liColCounter As Short Dim liControlCount As Short Dim lsValue As String dt = Session("Sample") For liCounter = 0 To dt.Rows.Count - 1 For liColCounter = 0 To dt.Columns.Count - 1 liControlCount = DataGrid1.Items(liCounter).Cells(liColCounter).Controls().Count - 1 If liControlCount <> -1 Then If DataGrid1.Items(liCounter).Cells(liColCounter).Controls(liControlCount).GetType Is GetType(TextBox) Then lsValue = CType(DataGrid1.Items(liCounter).Cells(liColCounter).Controls(liControlCount), TextBox).Text End If End If Next Next End Sub
Summary I hope this article explains a bit on how to create editable columns dynamically. Instead of TextBox you can also use controls like DropDown, ListBox etc. Waiting for your critics and Comments!!! Hope it helps. If you have any queries, please feel free to post here. Happy Coding!!!
Regards, Brainstorming Guys
|
| Author: Siddesh Kapadi 21 Apr 2005 | Member Level: Bronze Points : 0 |
this is working very fine. please it would have been better if u would have explained the code line by line. i m unable to understand the use of a button here as pressing it doesnot affect the output
|
| Author: Brainstorming Guy 21 Apr 2005 | Member Level: Diamond Points : 0 |
Hai Buddy, Thanks for your comments. I'm not getting what you are talking about "BUTTON PRESSING". I had written code for fetching the values when the button click is fired. You can fetch the values when the page is submitted. I just used only one variable. You can use as per ur requirement to more. If this is not what you expected, please feel free to pos tyour comments here again. No issues. I will try to give my explanation better. Happy Coding!!! Regards, Brainstorming Guys
|
| Author: rahul 04 Jun 2005 | Member Level: Bronze Points : 0 |
Code u have provided is working,but once we submit the button then after postback textboxes are not visible to me . if i add all the textboxes again then if my i am not changing the textbox.enable property then it is working fine but if i change the enable property then it loose it's older value .
Can u help out in this .
Thanks
|
| Author: swapna josyula 21 Nov 2005 | Member Level: Bronze Points : 0 |
THANK YOU for answering my queston? this code is for asp.net. but i am working with vb.net and would you please help me to do it.
|