narue1992
narue1992

Reputation: 1203

Add Gridview Rows

While referencing How to add gridview rows to a datatable?, I have tried to create a gridview based on an array.

So far my array [ email_list ] has strings where each index has 3 strings delimited by |.

Example:

Index1: 1231|Apple Company|[email protected]

Index2: 141411|KFC|[email protected]

To separate the strings I do the following [it does work]

 Dim strarray2() As String = email_list(i).Split("|"c)

  For Each str2 In strarray2
    Dim r As String = ""
    r = strarray2(y)
    y = y + 1
  Next

 ID = strarray2(0)
 company = strarray2(1)
 email = strarray2(2)

What I want:

I want to fill my gridview everytime I loop through the email_list array.

Issue:

As of right now, the strings get separated into 3 variables: ID, company, email and I debugged the strings and they do in fact work.

However; after the values are added to the gridview the gridview doesn't populate[aka doesn't show up on page]. This tells me that the way I am adding the new rows is wrong.

I tried doing:

For Each row As GridViewRow In DisplaySup.Rows
   If row.RowType = DataControlRowType.DataRow Then

outside my coding but since there is no rows currently made, all of my coding inside doesn't execute so I took it out.

Any suggestions on how to modify my following coding?

VB.NET

Dim dt As DataTable = New DataTable()

   dt.Columns.Add(New DataColumn("ID", GetType(String)))
   dt.Columns.Add(New DataColumn("Company Name", GetType(String)))
   dt.Columns.Add(New DataColumn("Email", GetType(String)))

        For i As Integer = 0 To email_list.Length - 2

            Dim y As Integer = 0
            Dim strarray2() As String = email_list(i).Split("|"c)

            For Each str2 In strarray2
                Dim r As String = ""
                r = strarray2(y)
                y = y + 1
            Next

            ID = strarray2(0)
            company = strarray2(1)
            email = strarray2(2)

            'ADD ONE ROW AT A TIME [my logic of what I want]
            Dim dr As DataRow = dt.NewRow()
            dr("ID") = Convert.ToString(ID)
            dr("Company Name") = Convert.ToString(company)
            dr("Email") = Convert.ToString(email)
            dt.Rows.Add(dr)

      Next

HTML:

<asp:GridView ID="DisplaySup" runat="server" align="center" 
Width="99%" AutoGenerateColumns="False"
BorderColor="Black" BorderStyle="Solid" DataKeyNames="ID" 
cssClass="grid_padding4" Font-Size="XX-Small">
   <Columns>

       <asp:BoundField DataField="ID" HeaderText="ID"> 
       <ItemStyle cssClass="grid_padding" width="15px"/>
       </asp:BoundField>

       <asp:BoundField DataField="company" HeaderText="Company Name" > 
       <ItemStyle cssClass="grid_padding" width="125px"/>
       </asp:BoundField>

       <asp:BoundField DataField="email" HeaderText="Owner Email" > 
       <ItemStyle cssClass="grid_padding" width="50px"/>
       </asp:BoundField>

    </Columns>

    <HeaderStyle BackColor="#BDBDAE" Font-Underline="False" 
       CssClass="linkNoUnderline" HorizontalAlign="Left" />
    <SelectedRowStyle BackColor="#99CCFF" />
</asp:GridView>

Upvotes: 0

Views: 809

Answers (2)

rheitzman
rheitzman

Reputation: 2297

Not sure why you are bothering with the datatable. Here is some code to display an array in an unbound DataGridView (dgv1.) It doesn't change the array contents so perhaps you were expecting to use the datatable for updates?

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    ' overhead for my testing
    Dim sData() As String = {"1231|Apple Company|[email protected]", "141411|KFC|[email protected]"}
    dgv1.DataSource = Nothing

    ' could be done in designer one time
    dgv1.Columns.Clear()
    dgv1.Columns.Add("ID", "ID")
    dgv1.Columns.Add("Company", "Company")
    dgv1.Columns.Add("URL", "URL")

    For Each s As String In sData
        dgv1.Rows.Add(s.Split("|"))
    Next
    dgv1.AutoResizeColumns()
    dgv1.ClearSelection()
End Sub

Upvotes: 0

haraman
haraman

Reputation: 2742

You have created the DataTable that is OK but have you assigned it to the GridView? Check all the comments in CAPITALS in code

Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("ID", GetType(String)))

'YOUR DATACOUMN NAME IN DATATABLE  AND  DATAFIELD OF BOUNDFIELD IN GRIDVIEW MUST MATCH
dt.Columns.Add(New DataColumn("company", GetType(String)))
dt.Columns.Add(New DataColumn("email", GetType(String)))

For i As Integer = 0 To email_list.Length - 2' SHOULD IT NOT BE email_list.Length - 1, IF LAST ONE IS NOT BLANK
    Dim y As Integer = 0
    Dim strarray2() As String = email_list(i).Split("|"c)

    'AS Namrehs POINTED IN COMMENTS, NO YOU REALLY DON'T NEED THAT "For Each str2 In strarray2" LOOP

    ID = strarray2(0)
    company = strarray2(1)
    email = strarray2(2)

    'ADD ONE ROW AT A TIME [my logic of what I want]
    Dim dr As DataRow = dt.NewRow()
    dr("ID") = Convert.ToString(ID)
    dr("company") = Convert.ToString(company)
    dr("email") = Convert.ToString(email)
    dt.Rows.Add(dr)
Next

'HERE YOU ALSO HAVE TO ASSIGN AND BIND IT TO THE GRIDVIEW
DisplaySup.DataSource = dt
DisplaySup.DataBind()

Upvotes: 1

Related Questions