?
Solved

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

Posted on 2013-11-10
3
Medium Priority
?
578 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 1000 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 1000 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

Industry Leaders: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 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