Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataGrid Update Woes
Message
General information
Forum:
ASP.NET
Category:
Web forms
Miscellaneous
Thread ID:
00700056
Message ID:
00700208
Views:
9
Hi Joe,

you bind data using the syntax similar to:
<Columns>
	<asp:TemplateColumn HeaderText="Customer ID">
		<HeaderStyle Wrap="False"></HeaderStyle>
		<ItemStyle Wrap="False"></ItemStyle>
		<ItemTemplate>
			<asp:Label id=lblCustID runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Cust_ID") %>'>
			</asp:Label>
		</ItemTemplate>
		<FooterStyle Wrap="False"></FooterStyle>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="Company">
		<HeaderStyle Wrap="False"></HeaderStyle>
		<ItemStyle Wrap="False"></ItemStyle>
		<ItemTemplate>
			<ASP:Label Text='<%# DataBinder.Eval(Container,"DataItem.Company") %>' runat="server" ID="lblCompany" />
		</ItemTemplate>
		<FooterStyle Wrap="False"></FooterStyle>
		<EditItemTemplate>
			<ASP:TextBox id="txtCompany" Text='<%# DataBinder.Eval(Container,"DataItem.Company") %>' runat="server" Font-Size="Large" Font-Names="Verdana" />
		</EditItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="Contact">
		<HeaderStyle Wrap="False"></HeaderStyle>
		<ItemStyle Wrap="False"></ItemStyle>
		<ItemTemplate>
			<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact") %>'>
			</asp:Label>
		</ItemTemplate>
		<FooterStyle Wrap="False"></FooterStyle>
		<EditItemTemplate>
			<asp:TextBox id="txtContact" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact") %>' Font-Size="Large" Font-Names="Verdana">
			</asp:TextBox>
		</EditItemTemplate>
	</asp:TemplateColumn>
	<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
		<HeaderStyle Wrap="False"></HeaderStyle>
		<ItemStyle Wrap="False"></ItemStyle>
		<FooterStyle Wrap="False"></FooterStyle>
	</asp:EditCommandColumn>
</Columns>
The syntax of
Text='<%# DataBinder.Eval(Container, "DataItem.Cust_ID") %>'
is where the real data binding is taking place. Here I am binding to the Cust_ID of my DataView that my DataGrid is bound to.

>I have a datagrid all decked out with my data using code. I am having mucho issues with the update method whos code is far too shamefull to show in public. I would also like to know how to bind the DS fields to the datagrid. I guess because I didn't use wizards I get punished. :( If I choose to automatically create columns all is well except my user can try to change my UID col...
>Please be patient as I have been working with .NET for < 1 week. I am MCPx2 VB6 dist/desktop I feel like a beginner again and my ego is quite sore.
>
>    Private Sub BindGrid(Optional ByVal SortField As String = "Eventdate")
>
>        Dim strUID, strSQL As String
>        strUID = DeCrypt(Session("UID"))
>        strSQL = "select Registry_ID as [ID] ,RegistryName As [Registry Name], RegistryDesc As [Description], " & _
>                 "EventDate as [Event Date] from registry where registrant_ID ='" & strUID & "' order by '" & SortField & "'"
>
>        Dim DS As DataSet
>        Dim MyConnection As New SqlConnection()
>        Dim MyCommand As SqlDataAdapter
>
>        MyCommand = New SqlDataAdapter(strSQL, SQLcn)
>
>        DS = New DataSet("MyRegistryDS")
>        MyCommand.Fill(DS, "registry")
>
>        grdReg.DataSource = DS.Tables("registry").DefaultView
>        'Source.Sort = E.SortExpression
>        'grdReg.Columns(1).ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(1)
>
>
>        Page.DataBind()
>        MyConnection.Close()
>        strLastSortField = SortField
>    End Sub
>
>My Datagrid source looks like this: (My apologis for the mangled code but it was unmanagably wide in the preview.
>
><asp:datagrid id="grdReg" runat="server" Width="719px" Height="117px" Font-Names="Arial"
>BorderColor="#BE9A64" BorderStyle="None" CellSpacing="4" BorderWidth="1px"
>BackColor="#DEBA84" CellPadding="3" HorizontalAlign="Center" AllowPaging="True"
>AllowSorting="True" GridLines="Horizontal" PageSize="5"><SelectedItemStyle Font-Bold="True"
>ForeColor="White" BackColor="#738A9C"></SelectedItemStyle><ItemStyle Wrap="False"
>ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle><HeaderStyle Font-Size="Smaller" Font-
>Names="Arial" Font-Bold="True" Wrap="False" HorizontalAlign="Center" ForeColor="SaddleBrown"
>VerticalAlign="Middle" BackColor="#BE9A64"></HeaderStyle><FooterStyle ForeColor="#8C4510"
>BackColor="#F7DFB5"></FooterStyle>
><Columns><asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
>CancelText="Cancel" EditText="Edit Registry"></asp:EditCommandColumn></Columns>
><PagerStyle Font-Size="XX-Small" Font-Names="Arial Black" HorizontalAlign="Center"
>ForeColor="#8C4510" Position="TopAndBottom"
>Mode="NumericPages"></PagerStyle></asp:datagrid><STRONG></span></form>
>
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform