Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IIS and Windows Authentication to SQL 21 53
Jquey and footrable 2 34
c# LinkButton OnClientClick 2 33
Auto refresh part of an MVC5 View 7 53
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…
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 …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now