Solved

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

Posted on 2013-11-10
3
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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