Get Value from Datagrid (C#)

tweywey

New Member
I am trying to figure out how to pull a value out of a datagrid to place in a text box. Like this:<BR><BR>txtBox1.Text = Value from DataGrid2<BR><BR>Can anyone help me?<BR><BR>Thanks,<BR>Russto get a collection of DataGridItems. If you have the local docs installed, see:<BR>ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclassitemstopic .htm<BR><BR>There is a code example there, which goes as follows:<BR><BR><%@ Import Namespace="System.Data" %><BR> <BR><html><BR><script language="VB" runat="server"><BR> <BR> Dim Cart As DataTable<BR> Dim CartView As DataView<BR> <BR> Function CreateDataSource() As ICollection<BR> Dim dt As New DataTable()<BR> Dim dr As DataRow<BR> <BR> dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))<BR> dt.Columns.Add(New DataColumn("StringValue", GetType(String)))<BR> dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))<BR> <BR> Dim i As Integer<BR> For i = 0 To 9<BR> dr = dt.NewRow()<BR> <BR> dr(0) = i<BR> dr(1) = "Item " & i.ToString()<BR> dr(2) = 1.23 *(i + 1)<BR> <BR> dt.Rows.Add(dr)<BR> Next i<BR> <BR> Dim dv As New DataView(dt)<BR> Return dv<BR> End Function 'CreateDataSource<BR><BR><BR> Sub Page_Load(sender As Object, e As EventArgs)<BR> <BR> If Not IsPostBack Then<BR> ' Need to load this data only once.<BR> ItemsGrid.DataSource = CreateDataSource()<BR> ItemsGrid.DataBind()<BR> End If<BR> End Sub 'Page_Load<BR> <BR><BR> Sub Button_Click(sender As Object, e As EventArgs)<BR> <BR> Dim item As DataGridItem<BR> For Each item In ItemsGrid.Items<BR> Label1.Text &= "<BR>" & item.Cells(0).Text & _<BR> " " & item.Cells(1).Text & _<BR> " " & item.Cells(2).Text<BR> Next item<BR> End Sub 'Button_Click<BR> <BR></script><BR> <BR><body><BR> <BR> <form runat=server><BR><BR> <h3>DataGrid Items Collection Example</h3><BR> <BR> <asp:DataGrid id="ItemsGrid" runat="server"<BR> BorderColor="black"<BR> BorderWidth="1"<BR> CellPadding="3"<BR> ShowFooter="true"<BR> AutoGenerateColumns="true"><BR><BR> <HeaderStyle BackColor="#00aaaa"><BR> </HeaderStyle><BR><BR> <FooterStyle BackColor="#00aaaa"><BR> </FooterStyle><BR> <BR> </asp:DataGrid><BR> <BR> <BR><BR><BR> <asp:Button id="Button1"<BR> Text="Display Contents of Items Collection"<BR> OnClick="Button_Click"<BR> runat="server"/><BR><BR> <BR><BR> <BR> <asp:Label id="Label1" <BR> runat="server"/><BR> <BR> </form><BR> <BR></body><BR></html><BR><BR>
 
Back
Top