Finding Data from a Datagrid

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Hi, i have what i consider a silly problem..... I'm trying to find the data that is in a certain cell in the datagrid. I was using the below

Visual Basic:
        Label20.Text = dgDepartments.Items(2).Cells(2).Text

Now i SWEAR that was working, i've come back to it after playing around with something else and its not working... however i am SO sure it was...

Firstly, could someone please tell me whether that would be the right code to retrieve the data and put it into the label. Currently this doesnt work, and i cant for the life of me figure out why, it doesnt cause an error, it just displays blank. And yes there is data in the table even after allowing for arrays starting at 0.

Secondly, if this code is right, what could of stopped it from working?

Thanks.
 
I've even put this line in above it
Visual Basic:
dgDepartments.Items(2).Cells(2).ForeColor = Drawing.Color.Crimson

Which successfully changes the colour of the writing on that cell, it just cant find the text.... :(
 
Could someone please tell me how i can find the contents of a template field in a datagrid?

The code is below
Visual Basic:
 <asp:DataGrid ID="dgDepartments" runat="server" AutoGenerateColumns="False" OnEditCommand="dgDepartmentHead_Edit"
        OnCancelCommand="dgDepartmentHead_Cancel" OnUpdateCommand="dgDepartmentHead_Update"
        OnDeleteCommand="dgDepartmentHead_Delete" ShowFooter="True" OnItemCommand="DoCommand">
        <Columns>
            <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="OK" ButtonType="PushButton" />
            <asp:TemplateColumn HeaderText="Department">
                <ItemTemplate>
                    <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" runat="server" OnClientClick="javascript:return confirm('Are you sure you want to delete the department?')" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Button CommandName="Insert" Text="Add" ID="btnAdd" runat="server" />
                </FooterTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Department">
                <ItemTemplate>
                    <%#DataBinder.Eval(Container.DataItem, "Department")%>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtDepartmentNew" runat="server" />
                </FooterTemplate>
                <ItemStyle Width="80%" />
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="DepartmentHead">
                <ItemTemplate>
                    <%#DataBinder.Eval(Container.DataItem, "DepartmentHead")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddDepartmentHead" runat="server" DataTextField="DepartmentHead"
                        DataValueField="username" DataSource='<%# GetDepartmentHeads() %>' SelectedIndex='<%# GetSelIndex(Container.DataItem("DepartmentHead")) %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddDepartmentHeadNew" runat="server" DataTextField="DepartmentHead"
                        DataValueField="username" DataSource='<%# GetDepartmentHeads() %>' />
                </FooterTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>
 
Last edited by a moderator:
Back
Top