set the value of inputbox inside a data grid through VB code - asp.net

I have a asp datagrid one of the column is having one input
<asp:DataGrid ID="dgItem" runat="server" Width="100%" CssClass="TableList" AutoGenerateColumns="False" PagerStyle-Visible="False">
<Columns>
<asp:TemplateColumn HeaderText="Disc %">
<HeaderStyle HorizontalAlign="Right" CssClass="ListHeader"></HeaderStyle>
<ItemStyle HorizontalAlign="Right" Width="6%" CssClass="TdList"></ItemStyle>
<ItemTemplate>
<input class="Input" onkeypress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode != 46 && event.keyCode != 45) event.returnValue=false;"
id="txtDiscRate" style="width: 100%; text-align: right" value="0.00" name="txtDiscRate"
runat="server" onchange="checkrate();">
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:DataGrid>
how to set the value of that column through VB.net code
This column is the 10th cell. In the 1st cell there is a value which should be set to the 10th column
So, I've tried the following..but not working.
Dim lnDiscRate As Double
Double.TryParse(lodgGrid.Cells(1).Text, lnDiscRate)
Dim loDiscRate As HtmlInputText
For Each lodgGrid In dgItem.Items
loDiscRate = lodgGrid.Cells(10).FindControl("txtDiscRate")
loDiscRate.Value = lnDiscRate 'Not working :(
loDiscRate.Enabled = True 'this is Working..
Next
Pls Help!!
Got something else. when i do this
CType(lodgGrid.FindControl("txtDiscRate"), HtmlInputText).Value = 10 ' Working
CType(lodgGrid.FindControl("txtDiscRate"), HtmlInputText).Value = lnDiscRate ' Not Working

Implement ItemDataBound on your DataGrid and use the following
Private Sub dgItem_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dgItem.ItemDataBound
'Get the text value of the second cell as stated above, if you want the first use 0
Dim lnDiscRate As String
lnDiscRate = e.Item.Cells(1).Text
'set the text value of the desired input to the string found above...
CType(e.Item.FindControl("txtDiscRate"), HtmlInputText).Value = lnDiscRate
End Sub

Related

Check for expired date in Gridview

I'm fairly new to programming and adjusting some code already written for me. I would like to check if a date in a Gridview(named: playDate) is older than the current date and if so the registration page icon will have it's visibility set to false.
But I'm getting the error:
BC30311: Value of type 'System.Web.UI.Control' cannot be converted to 'Date'
The error appears to be on this line:
playDate = r.Cells(1).FindControl("playDate")
Here's the Front end code:
<asp:SqlDataSource ID="DSCompetitions" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT tblCompetitions.compID, tblCompetitions.compName, tblCompetitions.playDate, tblCompetitions.venue, tblCompetitions.entryPrice, tblCompetitions.rules, tblCompetitions.maxPlayers
FROM tblCompetitions
ORDER BY tblCompetitions.playDate DESC"></asp:SqlDataSource>
<asp:Gridview ID="gdvCompetitions" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="compID" DataSourceID="DSCompetitions" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:BoundField DataField="compName" HeaderText="Event" />
<asp:BoundField DataField="playDate" DataFormatString = "{0:dd/MM/yyyy}" HeaderText="Date" />
<asp:BoundField DataField="venue" HeaderText="Venue" />
<asp:BoundField DataField="entryPrice" HeaderText="Entry Fee £" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HyperLink ID="hypView" runat="server" NavigateUrl='<%# "~/competition-view.aspx?compID=" & Eval("compID").ToString & "&round=1" %>'><asp:Image ID="ImgView" Width="30px" runat="server" ImageUrl="~/files/images/icons/view-icon.png" /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HyperLink ID="hypBook" runat="server" NavigateUrl='<%# "~/competition-book.aspx?compID=" & Eval("compID").ToString %>'><asp:Image ID="ImgRegister" Width="30px" runat="server" ImageUrl="~/files/images/icons/register-icon.png" /></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's My VB Code:
For Each r As GridViewRow in gdvCompetitions.Rows
If r.RowType = DataControlRowType.DataRow Then 'Execute the code only for datarow, excluding footer and header
Dim playDate As Date
playDate = r.Cells(1).FindControl("playDate")
Dim hypBook As Hyperlink
hypBook = r.Cells(5).FindControl("hypBook")
If date.now > playDate Then
hypBook.visible=false
End If
End If
Next r
You defined playDate as Date then initiated it with Control by playDate = r.Cells(1).FindControl("playDate") , it is not correct.
FindControl will find a control, as you know we can't initiate datetime variable with ASP.Net controls so
Change your code to :
For Each r As GridViewRow in gdvCompetitions.Rows
If r.RowType = DataControlRowType.DataRow Then 'Execute the code only for datarow, excluding footer and header
Dim playDate As Date
playDate = Convert.ToDateTime(r.Cells(1).Text)
Dim hypBook As Hyperlink
hypBook = r.Cells(5).FindControl("hypBook")
If date.now > playDate Then
hypBook.visible=false
End If
End If
Next r
I am not VB.Net programmer, But I am sure my explanation are right and changed code also works.
You'll need to take the value of your control, attempt to convert it to a date ("attempt" because you never know if a non-date could make its way into this column), and then do the comparison.
var playDate = DateTime.Parse(r.Cells(1).Text); // this could throw an error, research DateTime.TryParse()
if (DateTime.Now > playDate)
{
// then...
}
Sorry. Just realized your question is VB. Telerik's code converter is amazing for going from C# to VB, and vice versa.
Instead of
playDate = r.Cells(1).FindControl("playDate")
Use
playDate = r.Cells(1).Text;
FindControl will only work if you have any asp control in grid. For boundfield, you need to get the value of cell.

Editing GridView For dynamically generated Field

My GridView is as follows
It has EmptyDatatemplate and Command Field
<asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true" style="table-layout:fixed;" Width="2000px" RowStyle-HorizontalAlign="Left">
<EmptyDataTemplate>
</EmptyDataTemplate>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit">
<ItemStyle Font-Bold="true" Font-Size="Small" />
<HeaderStyle CssClass="AAddOn" />
</asp:CommandField>
</asp:GridView>
GridView Looks like
Name Age Country
A 10 NNN Edit
B 23 NNN Edit
Now if i click on edit i need only Age Column to be edited
If i give AGridView.EditIndex = e.NewEditIndex entire row is edited
In codebehind
Private Sub AGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles AGridView.RowEditing
Try
AGridView.EditIndex = e.NewEditIndex
AGridView.DataSource = SessionItems.ADataset.Tables("AHello")
AGridView.DataBind()
Catch ex As Exception
SetErrorMsg(ex.Message.ToString, "Error")
End Try
End Sub
That's simple. Whatever the columns you don't want editable, just add those in DataKeyNames property of grid. Try this.
<asp:GridView ID="AGridView" runat="server" AutoGenerateColumns="true" style="table-layout:fixed;" Width="2000px" RowStyle-HorizontalAlign="Left" DataKeyNames = "Name,Country" >
<EmptyDataTemplate>
</EmptyDataTemplate>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="80px" EditText="Edit">
<ItemStyle Font-Bold="true" Font-Size="Small" />
<HeaderStyle CssClass="AAddOn" />
</asp:CommandField>
</asp:GridView>
Private Sub AGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles AGridView.RowEditing
Try
Dim colsToNotEdit As List<string>
//C# code
for (int i = 0; i < AGridView.HeaderRow.Cells.Count; i++)
{
if(AGridView.HeaderRow.Cells[i].Text !="age")
colsToNotEdit.Add(grdToDisplay.HeaderRow.Cells[i].Text);
}
AGridView.DataKeyNames = colsToNotEdit.ToArray()
AGridView.EditIndex = e.NewEditIndex
AGridView.DataSource = SessionItems.ADataset.Tables("AHello")
AGridView.DataBind()
Catch ex As Exception
SetErrorMsg(ex.Message.ToString, "Error")
End Try
End Sub
Another option would be to not use the AutoGenerateColumns functionality and specify your column templates.
This way you can specify the ItemTemplate, EditTemplate or FooterTemplate for each column.
So for a column that you dont want to edit, ie. Name use <asp:BoundField> and for age use:
<asp:TemplateField>
<ItemTemplate><asp:Label>Bind Me</asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox>Bind Me</asp:TextBox></EditItemTemplate>
</asp:TemplateField>
Edit: When using Dynamic Column Names
If you have a datatable that contains your data, lets say dtRowData.
You should have a piece of code where you assign the datatable to the grid:
GridView1.DataSource = dtRowData;
GridView1.DataBind();
You can add a loop in before the databind call as follows:
string DataKeyNames = "";
foreach (DataColumn dc in dtRowData.Columns)
{
if(dc.Name != "Age")
DataKeyNames += dc.Name + ",";
}
And then before Binding Set GridView1.DataKeyNames = DataKeyNames
You will probably need to do a substring on DataKeyNames to remove the trailing ,

Gridview Dropdownlist binding

I am having a lot of trouble getting a dropdown to bind with data from my database with the appropriate departments.
This is what I have so far:
HTML:
<asp:GridView ID="gridDepartmentHistory" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server" Visible="true" Text='<%# Eval("Department")%>'></asp:Label>
<asp:DropDownList ID="ddlDepartment" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Start Date" HeaderText="Start Date" />
<asp:BoundField DataField="End Date" HeaderText="End Date" />
</Columns>
</asp:GridView>
Code behind:
Protected Sub gridDepartmentHistory_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridDepartmentHistory.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ddlDepartment As DropDownList = CType(e.Row.FindControl("ddlDepartment"), DropDownList)
Dim list As ICollection(Of Department) = Department.hrGetDepartmentList() 'Class method to fill a collection of items with the Department's Name and ID
ddlDepartment.DataSource = list
ddlDepartment.DataTextField = "Name"
ddlDepartment.DataValueField = "ID"
ddlDepartment.DataBind()
Dim dept As String = CType(e.Row.FindControl("lblDepartment"), Label).Text
ddlDepartment.Items.FindByText(dept).Selected = True
End If
End Sub
When I run this it throws an exception saying:
Object reference not set to an instance of an object.
BTW: I am using this tutorial to help me through: http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx
Any help would be greatly appreciated! Thank you!
You simply need to retrieve dept id and store it in gridview as hidden (if you don't want to display it).
Dim dept As String = CType(e.Row.FindControl("lblDepartmentId"), Label).Text
ddlDepartment.SelectedValue = dept;
Hope it helps.
Your problem is that you ara trying to find the contorl in the row but is inside a table cell.
Try this.
Dim cbo As DropDownList = CType(YourDGV.Rows(x).Cells(0).FindControl("ddlDepartment"), DropDownList)

Gridview not sorting

I've come across this strange error and it's been bugging me for hours now.
I've got some LinkButtons on my screen and depending on which button the user chooses a label's text is set, a GridView is displayed and the Datasource of this GridView is assigned based on the text of the label.
This all works fine and dandy. However the problem arises when I want to sort this GridView.
At the moment, nothing happens. When I attempt to sort a column the page just refreshes and I'm left with the same unsorted mess in my GridView.
I've put my application into debug mode with breakpoints along the way and noticed that when I get to this step (the full code can be seen at the bottom of this post) :
senderGridView.SortExpression = button.CommandArgument
the senderGridView.SortExpression is "" so for some reason it's not passing the sort expression.
Hopefully somebody can enlighten me as to why my grid isn't sorting as I'm sure it's just a stupid mistake somewhere but any help would be appreciated, thanks.
Code that you may require...
My sub that sets the sort images when a row is created can be seen below:
Protected Sub GridViewSortImages(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim senderGridView As GridView = CType(sender, GridView)
'Loop through each cell in header row
For Each cell As TableCell In e.Row.Cells
If cell.HasControls Then
Dim button As LinkButton = TryCast((cell.Controls(0)), LinkButton)
Dim gv As New HtmlGenericControl("div")
Dim lnkName As New Label()
'Test cell to see if a link button exists (i.e. Allow Sorting = True)
If Not (button Is Nothing) Then
'Create new label and image and set to standard unsorted view
lnkName.Text = button.Text
Dim image As New System.Web.UI.WebControls.Image
image.ImageUrl = "images/sort-1x1.png"
image.ToolTip = "Sort"
image.AlternateText = "Sort"
'Test to see if grid is already sorted, and apply relevant image & tooltip
If senderGridView.SortExpression = button.CommandArgument Then
If senderGridView.SortDirection = SortDirection.Ascending Then
image.ImageUrl = "images/sort-asc.png"
image.ToolTip = "Sort Descending"
image.AlternateText = "Sort Descending"
Else
image.ImageUrl = "images/sort-desc.png"
image.ToolTip = "Sort Ascending"
image.AlternateText = "Sort Ascending"
End If
End If
'Add label and image to new div
gv.Controls.Add(lnkName)
gv.Controls.Add(image)
'Replace original column header with new div
button.Controls.Add(gv)
End If
End If
Next
End Sub
My RowCreated sub for the GridView...
Protected Sub grvHomeRisk_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles grvHomeRisk.RowCreated
If Not (e.Row Is Nothing) AndAlso e.Row.RowType = DataControlRowType.Header Then
GridViewSortImages(sender, e) 'Call sort code
End If
End Sub
My Sorting sub...
Protected Sub grvHomeRisk_Sorting(sender As Object, e As GridViewSortEventArgs) Handles grvHomeRisk.Sorting
Select Case lblBreachHeaderb.Text
Case "Current Risks"
grvHomeRisk.DataSource = SQLDS_ListCurrentRisk
Case "All Risks Overdue"
grvHomeRisk.DataSource = SQLDS_ListAllRiskOverdue
Case "My Risks"
grvHomeRisk.DataSource = SQLDS_ListMyRisk
Case "My Risks Overdue"
grvHomeRisk.DataSource = SQLDS_ListRiskOverdue
Case "Risks Requested to Score & Re-Score"
grvHomeRisk.DataSource = SQLDS_ListScores
Case "Risks Requested to Score"
grvHomeRisk.DataSource = SQLDS_ListRiskScores
Case "Risks Requested to Re-Score"
grvHomeRisk.DataSource = SQLDS_ListRiskReScores
End Select
grvHomeRisk.DataBind()
End Sub
My GridView...
<asp:GridView ID="grvHomeRisk" runat="server" AutoGenerateColumns="false" AllowSorting="true"
CssClass="GridMain" UseAccessibleHeader="false"
ForeColor="#333333" GridLines="None" Width="780px" BorderWidth="0px"
AllowPaging="true" PageSize="5" CellPadding="3" DataKeyNames="IDRISK">
<Columns>
<asp:CommandField ButtonType="Image" SelectText="View Risk" ShowSelectButton="True" SelectImageUrl="~/Images/button-select1.png" />
<asp:BoundField DataField="TXRISKSUMMARY" HeaderText="Risk" ReadOnly="True" />
<asp:BoundField DataField="TSRISKSTART" HeaderText="Start Date" ReadOnly="True" DataFormatString="{0:dd MMM yyyy}" />
<asp:BoundField DataField="TSRISKREVIEW" HeaderText="Review Date" SortExpression="TSRISKREVIEW" ReadOnly="True" DataFormatString="{0:dd MMM yyyy}" />
<asp:BoundField DataField="TXUSER_NAME_O" HeaderText="Owner" SortExpression="TXUSER_NAME_O" ReadOnly="True" />
<asp:BoundField DataField="TXDESCRIPTION" HeaderText="Risk Element" SortExpression="TXDESCRIPTION" ReadOnly="True" />
<asp:BoundField DataField="TSDATMOD" HeaderText="Date Modified" ReadOnly="True" DataFormatString="{0:dd MMM yyyy}" />
<asp:BoundField DataField="TXUSER_NAME_M" HeaderText="Modified By" ReadOnly="True" />
</Columns>
<PagerTemplate>
<table>
<tr>
<td>
<asp:Button ID="FirstButton" runat="server" CommandArgument="First" CommandName="Page" Text="<<" CssClass="buttongrid" ToolTip="First" OnClientClick="needToConfirm = false;" />
<asp:Button ID="PrevButton" runat="server" CommandArgument="Prev" CommandName="Page" Text="Previous" CssClass="button" ToolTip="Previous" OnClientClick="needToConfirm = false;" />
</td>
<td>
<asp:Label ID="lblPageCount" runat="server" CssClass="TextA12Bold" Visible="false"></asp:Label>
</td>
<td>
<asp:Button ID="NextButton" runat="server" CommandArgument="Next" CommandName="Page" Text="Next" CssClass="button" ToolTip="Next" OnClientClick="needToConfirm = false;" />
<asp:Button ID="LastButton" runat="server" CommandArgument="Last" CommandName="Page" Text=">>" CssClass="buttongrid" ToolTip="Last" OnClientClick="needToConfirm = false;" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>

Calling Data From GridView

I have a GridView with a bunch of DynamicFields like so;
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource">
<Columns>
<asp:DynamicField HeaderText="Date Submitted" DataField="DATE_CREATED" />
<asp:DynamicField HeaderText="Assigned To" DataField="ASSIGNED_TO" />
<asp:DynamicField HeaderText="Active Account" DataField="Active_Account" />
<asp:DynamicField HeaderText="Client ID" DataField="CLIENT_ID" />
<asp:DynamicField HeaderText="Client Type" DataField="CLIENT_Type" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="GridDataSource" OnSelected="TotalRows" runat="server"
EnableDelete="true">
<WhereParameters>
<asp:DynamicControlParameter ControlID="FilterRepeater" />
</WhereParameters>
</asp:EntityDataSource>
Now it displays fine, the right data comes to screen but when I try to access that data using a row number I'm always finding blank cells. For example I tried the following to check every cell but to no avail;
Dim x As Integer = 0
Dim y As Integer = 0
While x < GridView1.Rows.Count
While y < GridView1.Rows(x).Cells.Count
If Not (GridView1.Rows(x).Cells(y).Text = "") Then
MsgBox(String.Format("{0},{1},{2}", x.ToString, y.ToString,
GridView1.Rows(x).Cells(y).Text))
End If
y = y + 1
End While
x = x + 1
y = 0
End While
No message box displayed so all the cells are empty strings. But I can clearly see they're populated on screen! What am I doing wrong?
UPDATE
After Pilgerstorfer Franz suggestion that I look for Textbox elements I used the following code which basically looks at all the cells in the table and try's to pull data out of them and if it's not blank then display a msgbox (also informs me of any new controls I haven't accounted for);
If GridView1.Rows(0).RowType = DataControlRowType.DataRow Then
For r = 0 To GridView1.Rows.Count - 1
For c = 0 To (GridView1.Rows(r).Cells.Count - 1)
Dim cell = GridView1.Rows(r).Cells(c)
For b = 0 To cell.Controls.Count - 1
If (cell.Controls(b).GetType() Is GetType(TextBox)) Then
Dim td = CType(cell.Controls(b), TextBox)
Text = td.Text.Trim
ElseIf (cell.Controls(b).GetType() Is GetType(LiteralControl)) Then
Dim td = CType(cell.Controls(b), LiteralControl)
Text = td.Text.Trim
ElseIf (cell.Controls(b).GetType() Is GetType(DynamicControl)) Then
Dim td = CType(cell.Controls(b), DynamicControl)
Text = td.Table.Columns.Item(c).DisplayName()
Else
MsgBox(String.Format("New Control of type: {0}", cell.Controls(b).GetType().FullName))
End If
If Not Text = "" Then
MsgBox(String.Format("{0},{1},{2}", c.ToString, b.ToString, Text))
End If
Next
Next
Next
End If
Unfortunately most cells just contained DynamicControl and since I was putting the DisplayName into Text I was just getting the column header every time. So how do I get the text value property out of a DynamicControl?
Additional Info
I'm further confused by this problem because since this is a project that I'm updating there is initial code two lines of which are;
Dim UserID = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)
Dim clientType As String = GridView1.DataKeys(e.RowIndex).Item(1).ToString
These successfully bring back UserID and ClientType. Now I don't really understand DataKeys but I tried using;
Dim clientType As String = GridView1.DataKeys(e.RowIndex).Item(Num).ToString
where Num increases by one every time expecting it to bring back the rest of the row data, but I simply got an index was out of range error.
ANOTHER UPDATE!!
Here is another bit of the ASPX page but I'n not entirely certain what it does. Pilgerstorfer Franz created some code to look for the textboxes created by the dynamicControl. Now I'm wondering if this code here causes other types of controls to be used rather than Textboxes. Thoughts?
<asp:FilterRepeater ID="FilterRepeater" runat="server" Visible="false">
<ItemTemplate>
<h2><asp:Label ID="lblDisplay" runat="server" Text='<%#
Eval("DisplayName") %>' AssociatedControlID="DynamicFilter$DropDownList1" /></h2>
<asp:DynamicFilter runat="server" ID="DynamicFilter"
OnSelectedIndexChanged="OnFilterSelectedIndexChanged" />
</ItemTemplate>
<FooterTemplate><br /><br /></FooterTemplate>
</asp:FilterRepeater>
According to my assumptions (see comments) I did a small demo...
<asp:DynamicDataManager ID="DynamicDataManager1" runat="server">
<DataControls>
<asp:DataControlReference ControlID="GridView1" />
</DataControls>
</asp:DynamicDataManager>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ContactID" DataSourceID="EntityDataSource1" AllowPaging="true"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:DynamicField DataField="ContactID" ReadOnly="true" HeaderText="ContactID" />
<asp:DynamicField DataField="Title" HeaderText="Title" />
<asp:DynamicField DataField="FirstName" HeaderText="FirstName" />
<asp:DynamicField DataField="LastName" HeaderText="LastName" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=dbTestIT34_EFEntities"
DefaultContainerName="dbTestIT34_EFEntities" EnableFlattening="False" EnableUpdate="True"
EntitySetName="Contacts" EntityTypeFilter="Contact">
</asp:EntityDataSource>
And according to my data storage I did handle the rowUpdating event. Unfortunatelly accessing any control within a dynamic control is not easy. So I did a very quick and dirty hack...
protected void Page_Init(object sender, EventArgs e)
{
GridView1.EnableDynamicData(typeof(Contact));
EntityDataSource1.ContextType = typeof(dbTestIT34_EFEntities);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int row = 0;
int firstNameColumn = 2;
while (row < GridView1.Rows.Count)
{
// sorry, a little bit confusing ...
// as dynamic control generates other controls dynamically,
// this was the very first thing that worked for me!
TextBox t = GridView1.Rows[row].Cells[firstNameColumn].Controls[0].Controls[0].Controls[0] as TextBox;
if (t != null)
Debug.WriteLine(t.Text);
row++;
}
}
Hope that helps!
Iterating through the controls never worked for me so in the end I gave up and just queried the DB again... Eh it works.

Resources