Hello

I currently have a Datagridview which takes data from an XML file.

I want to display the array of numbers.

Here is the code for my GridView:

<asp:GridView ID="rd" runat="Server" OnRowEditing="Editdata" OnPageIndexChanging="pageddata"
        OnRowDeleting="Deletedata" OnRowUpdating="Updatedata" OnRowCancelingEdit="Canceldata"
        AllowPaging="True" AutoGenerateColumns="False" PageSize="30" CellPadding="4"
        ForeColor="#333333" GridLines="None">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:TemplateField HeaderText="DataField">
                <ItemTemplate>
                    <asp:Label ID="lblDataField" runat="server" Text='<%#Eval("DataField") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtDataField" runat="server" ReadOnly="true" Visible="true" Text='<%#Eval("DataField") %>'></asp:TextBox>
                    <%-- Visible=false to hide textbox, ReadOnly=true to make textbox ReadOnly --%>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header">
                <ItemTemplate>
                    <asp:Label ID="lblHeader" runat="server" Text='<%#Eval("Header") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtHeader" runat="server" Text='<%#Eval("Header") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="UseDefaults">
                <ItemTemplate>
                    <asp:Label ID="lblUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" ButtonType="Button" />
            <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
        </Columns>
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />

    </asp:GridView>

Now what I want to do know is how can I obtain an array of the DataField column. For example it has A01, A02, A03 I need to check how many entries exist and them put them into a textbox for example.

Is this possible?

I've currently used this to take the last 'DataFeild' value from the gridview:

void binddata()
{
    DataSet rdawTest = new DataSet();
    rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
    foreach (DataColumn da in rdawTest.Tables[0].Columns)
    {
        da.ColumnMapping = MappingType.Attribute;
    }
    rd.DataSource = rdawTest;        
    rd.DataBind();

    DataSet ds = rd.DataSource as DataSet;
    DataRow dr = ds.Tables[0].NewRow();

    int index = rd.Rows.Count - 1;

    GridViewRow gRow = rd.Rows[index];

    String str = ((Label)(gRow.Cells[0].Controls[1])).Text;

    txtArray.Text = str;

}

Thank you.

Edited 3 Years Ago by diafol: fixed formatting

Do not understand anything you are saying.
Perhaps ask in on the ASP forum?
Do you mean to convert a Column of a DataGridView to an array?

hi, yes it would be good if you explain this a bit better. I have a few ideas of what do you want, but I will not go and describe all here now.
So..

Yes sorry I wanted to convert a Column of a datagridview into an array.

Try this code:

string[] columnData = new string[dataGridView1.Rows.Count];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[i] as DataGridViewRow;
                columnData[i] = row.Cells["columnName"].Value.ToString();
            }

Hello, the above code now looks like this:

It is for a web application not a windows form - which I realise is in the wrong area.

string[] columnData = new string[rd.Rows.Count]; 
        for (int i = 0; i < rd.Rows.Count; i++) 
         { 
         GridViewRow row = rd.Rows[i] as GridViewRow;
          columnData[i] = row.Cells["SortOrder"].Value.ToString(); 
         }

The last line of code however gives me an error:

error CS1502: The best overloaded method match for 'System.Web.UI.WebControls.TableCellCollection.this[int]' has some invalid arguments

Edited 5 Years Ago by dawsonz: n/a

This article has been dead for over six months. Start a new discussion instead.