Solved

GridView how to set behind code to able update rows, or columns?

Posted on 2013-11-10
3
563 Views
Last Modified: 2013-11-14
Hello,
I don't have problem to populate GridView behind code. But, now I need also to edit/update rows/columns, but just don't know where to start.
Actual, I have enable AutoGenerateEditButton, and I thought that asp.net .dll will do the rest job :-(.
I would be please if someone could give me a tip, sample, etc :-)

    Dim connection As New SqlConnection("<conn string>")
                connection.Open()
                Dim da As New SqlDataAdapter("<query>", connection)
                Dim dt As New DataTable()
                da.Fill(dt)
                GridView1.DataSource = dt.DefaultView
                GridView1.AutoGenerateColumns = True
                [b]GridView1.AutoGenerateEditButton = True
[/b]                GridView1.DataBind()

Open in new window


Thanks in advance for help!
0
Comment
Question by:dejandejanovic
3 Comments
 
LVL 12

Assisted Solution

by:duttcom
duttcom earned 250 total points
ID: 39637631
Try adding a commandfield column in your gridview to show the buttons. The example below shows the commandfield in the first column and shows the Edit and Delete buttons, but you could put it last if you want.

<Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="Field1" HeaderText="Field header text" ReadOnly="True" SortExpression="Field1" />

Etc...
0
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 250 total points
ID: 39637999
hi..
Here is the markup you need to create an EditItemTemplate within your TemplateField

<asp:GridView ID="GridView2" runat="server" Visible="False" AutoGenerateColumns="false"
              ShowFooter="True"
              AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Action Item">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" Width="500px" TextMode="MultiLine" runat="server"></asp:TextBox>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox id="EditActionItemTextBox" runat="server" Width="500px" TextMode="MultiLine"></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="ButtonAdd" OnClick="ButtonAdd_Click" runat="server" Text="Add New Row" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Now in your code-behind where you are updating your data table, do this

'Update the values.
Dim row = GridView2.Rows(e.RowIndex)
dt.Rows(row.DataItemIndex)("RowNumber") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
dt.Rows(row.DataItemIndex)("Action Item") = (CType((row.FindControl("EditActionItemTextBox")), TextBox).Text)

Note: In your <ItemTemplate> you may want to consider making the control read-only so that your users know they cannot change the value, usually when a user sees a text box they think they can edit the content, but it completely makes sense to have an editable text box in edit mode of your grid view. To change the the <ItemTemplate> text box to read-only, just add ReadOnly="True", like this

<ItemTemplate>
    <asp:TextBox ID="TextBox1" Width="500px" TextMode="MultiLine"
                 runat="server" ReadOnly="True"></asp:TextBox>
</ItemTemplate>

Depending upon the browser, the control will have a slight gray tinge to it. The user will be able to click on the text, but will not be able to change the text.
0
 

Author Comment

by:dejandejanovic
ID: 39640818
Hm,
well my actual goal is to populate gridview by different tables, different table structure. That why I'm looking to design gridview from code behind.
Like:
- first populate gridview with table columns A, B, C, Edit column
- enable edit column with update query (also behind code)
- delete query is no needed

That would not be a problem, but my main problem is that I cannot have a STATIC GridView as tables have different columns.

For edit:
GridView1.EditIndex = e.NewEditIndex;

Open in new window

For cancel:
GridView1.EditIndex = -1

Open in new window


But, now I also need to populate row on edit, so selection query >> and update row with update query.

Just in case if you have any samples, but if not then we can close this case.

Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now