Solved

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

Posted on 2013-11-10
3
570 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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