How do I add a new column to my already bound Gridview?

I've got a VB 2015 / ASP app that features a Gridview  displaying 6 columns of data via a SQL query.
I wish to add a new column at runtime, and poke in a text string based off cell text in column1(0 based)
Here's what my aspx Gridview section looks like:
         
   <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="486px" Width="661px">
                <columns>
                    <asp:BoundField DataField="spid" HeaderText="spid" SortExpression="spid" />
                    <asp:BoundField DataField="Login" HeaderText="Login" ReadOnly="True" SortExpression="Login" />
                    <asp:BoundField DataField="DB" HeaderText="DB" ReadOnly="True" SortExpression="DB" />
                    <asp:BoundField DataField="SERVER" HeaderText="SERVER" SortExpression="SERVER"/>
                    <asp:BoundField DataField="LOGIN TIME" HeaderText="LOGIN TIME" ReadOnly="True" SortExpression="LOGIN TIME"/>
                    <asp:BoundField DataField="USER COUNT" HeaderText="USER COUNT" ReadOnly="True" SortExpression="USER COUNT"/>
                    <asp:TemplateField HeaderText="SITE"   runat="server"></asp:TemplateField>
                </columns>
            </asp:GridView>

Open in new window


In my code behind, I have a For loop where I'm doing some gridview manipulation such as cell coloring..etc
and I thought maybe I could do something similar to:
For i = 0 To GridView1.Rows.Count - 1
nameToCheck = GridView1.Rows(i).Cells(1).Text
nameToCheck = nameToCheck.Trim()

If nameToCheck = "MYNAME" Then GridView1.Rows(i).Cells(5).Text = "ME"

Open in new window


Here's a snippet of what the grid looks like at runtime:
Capture.PNG
I want to query cell#2 for a value, and depending on the result, poke some text into my new column/cell...
make sense?
Am I even remotely close??
Rich
Richard GouetteIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please use like below -
BoundField mynewcolumn= new BoundField();
mynewcolumn.DataField = "Field Name from Datatable";
mynewcolumn.Headertext = "New column Header";
GridView1.Columns.Add(mynewcolumn);

Open in new window

0
Richard GouetteIT ManagerAuthor Commented:
I did something similar:
        Dim site_field As New BoundField()
        site_field.HeaderText = "SITE"
        GridView1.Columns.Add(site_field)

I have a Column header showing in column#6, but, cannot figure out the binding portion, etc
0
Pawan KumarDatabase ExpertCommented:
The value you want to show in the columns is coming from datatable ?
That value needs to be bind.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Richard GouetteIT ManagerAuthor Commented:
No, it would be some text that I'm giving it explicitly in code behind.
Like:

If nameToCheck = "MYNAME" Then GridView1.Rows(i).Cells(5).Text = "ME"

Open in new window

0
Pawan KumarDatabase ExpertCommented:
Please try this-

Dim site_field As New BoundField()
site_field.HeaderText = "SITE"
site_field.DataField= "ME"
GridView1.Columns.Add(site_field)

Open in new window

0
Richard GouetteIT ManagerAuthor Commented:
When I try that code under my Page_Load event, the gridview disappears..

Is it even possible to bind a new column to my existing gridview, if the gridview is already  bound to a SQL DataSource?
0
Pawan KumarDatabase ExpertCommented:
Add a hardcoded condition to your SQL itself like below-

SELECT yourcolumns, 'ME' extracol
FROM yourtableName

then you can use like

im site_field As New BoundField()
site_field.HeaderText = "SITE"
site_field.DataField= dt["extracol"]
GridView1.Columns.Add(site_field)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard GouetteIT ManagerAuthor Commented:
is dt a DataTable?
I do not have any DataTables in the project
0
Richard GouetteIT ManagerAuthor Commented:
I added 'siteName' as site_Name to my query, and this to my GridView definition:
                    <asp:BoundField DataField="site_Name" HeaderText="SITE" SortExpression="SITE"/>

Open in new window


at runtime I'm getting:
Capture.PNG
That column should be column#5
so, I'm trying:
  GridView1.Rows(i).Cells(5).Text = "MEEEE"

Open in new window

inside of my For loop through the rows.. but I get what you see in the picture

Seems like we're a step closer maybe..
Capture.PNG
0
Richard GouetteIT ManagerAuthor Commented:
All set. I got it working: part of the problem was using the wrong cell index# during testing.
I'm using the newly created field in SQL query, and:
        Dim site_Name As New BoundField() 'site_Name
        GridView1.Columns.Add(site_Name)
        site_Name.HeaderText = "SITE"
        site_Name.DataField = "site_Name"

thanks!
Rich
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.