Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with datagrid updating

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
using the yellow code I get error
Object reference not set to an instance of an object.
using the green code I get error
Value of type 'System.Web.UI.WebControls.GridViewRow' cannot be converted to 'Integer'
I got this off some site which was written in c# originally
Is there a way to convert "row" so it will work?

Code:
Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        'Retrieve the table from the session object.
        Dim dt = CType(Session("AwardType"), DataTable)

        'Update the values.
        Dim row = GridView1.Rows(e.RowIndex)
        dt.Rows(row.DataItemIndex)("UniqueID") = GridView1.Rows([highlight #FCE94F]GridView1.SelectedIndex[/highlight]).Cells(2).Text
        dt.Rows(row.DataItemIndex)("UniqueID") = GridView1.Rows([highlight #8AE234]row[/highlight]).Cells(2).Text
        dt.Rows(row.DataItemIndex)("AwardTypes") = GridView1.Rows(GridView1.SelectedIndex).Cells(1).Text

        'Reset the edit index.
        GridView1.EditIndex = -1

        'Bind data to the GridView control.
        GridView1.DataBind()

    End Sub

DougP
 
Check this link:
Code:
  Protected Sub TaskGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    'Retrieve the table from the session object.
    Dim dt = CType(Session("TaskTable"), DataTable)

    'Update the values.
    Dim row = TaskGridView.Rows(e.RowIndex)
    dt.Rows(row.DataItemIndex)("Id") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
    dt.Rows(row.DataItemIndex)("Description") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
    dt.Rows(row.DataItemIndex)("IsComplete") = (CType((row.Cells(3).Controls(0)), CheckBox)).Checked

    'Reset the edit index.
    TaskGridView.EditIndex = -1

    'Bind data to the GridView control.
    BindData()
  End Sub
 
been there done that; it does not work either.

this line does not work in .NET 4.
CType((row.Cells(1).Controls(0)), TextBox)).Text




DougP
 
You'll have to debug. hit the first row and then use ro. cells(x)...etc and find out where it is failing

Not sure why you are doing it that way anyway. Youshould set AutoGenerateColumns to False and create the columns in the markup manually using template columns.
Then in your codebehind do a DirectCast(row.FindControl("YourTextBox"),TextBox).Text
 
The issue here is that differnt methods don't use the samw way to return info. from the grid. In the Selected index change: this works fine.
I just copied it now out of a working program.
Code:
    Protected Sub GridViewNames_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewNames.SelectedIndexChanged

        Dim Lname As String = ""
        Dim Fname As String = ""
        gblGridSelectedIndex = GridViewNames.Rows(GridViewNames.SelectedIndex).Cells(3).Text
        Lname = GridViewNames.Rows(GridViewNames.SelectedIndex).Cells(1).Text
        Fname = GridViewNames.Rows(GridViewNames.SelectedIndex).Cells(2).Text

but in the RowUpdating it does not. gives this:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Code:
    Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

        Dim dt = CType(Session("AwardType"), DataTable)  
        Dim UniqueID As Integer = 0
        Dim AwardType As String = ""
        Session("AwardsSelectedIndex") = GridView1.Rows([highlight #FCE94F]GridView1.SelectedIndex[/highlight]).Cells(3).Text

Personally I think it a design flaw. Oh I'm sure there is some reason I don't understand, but they all should use the same thing. Right?
Its the same grid after all.

DougP
 
Personally I think it a design flaw. Oh I'm sure there is some reason I don't understand, but they all should use the same thing. Right?
Its the same grid after all.

No. All events pertain to a gridview, however, different properties exist only for certain events. A SelectedIndex property makes no sense in a row updating event, they give you the row property, because you are updating it, not selecting it. Could it be the same, probably, however it is done for a reason. As you can see there is a different event args collection for each event. That gives you what is available in each event. Take a look at what is available in "e" in each event. This is why I gave you specific code for the rowupdating event.
 
well I figured it out, was to remove all the code behind. none at all.
then make sure there is a DataKeyNames="UniqueID" in the HTML markup. everything else was added automatically in the SQLDatasource configurator.
you also have to add the SQL statements which was done in the SQLDatasource configurator as well.

Code:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Awards.aspx.vb" Inherits="Recognition.Awards" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        .style1
        {
            font-size: x-large;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p class="style1">
        <strong>Add Awards Screen</strong></p>
    <p>
&nbsp;Enter New Award
        <asp:TextBox ID="txtAward" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="btnAddAward" runat="server" Text="Add Award" />
    </p>
    <p>
        Note the &quot;Delete&quot; deletes with out warning. Click Edit to change a line item.</p>
 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                            DataSourceID="SqlDataSource1"
                            CellPadding="4" 
                            ForeColor="#333333" 
                            AllowPaging="True" 
                            AllowSorting="True"
                            [highlight #FCE94F]DataKeyNames="UniqueID"[/highlight]                            >

                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

                    <Columns>
                        <asp:CommandField ButtonType="Button" ShowEditButton="True" />
                        <asp:BoundField DataField="AwardTypes" HeaderText="AwardTypes" 
                            SortExpression="AwardTypes" />
                        <asp:BoundField DataField="UniqueID" HeaderText="UniqueID" 
                            InsertVisible="False" ReadOnly="True" Visible="false"  SortExpression="UniqueID" />
                        <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
                    </Columns>
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SOWTimeReportingConnectionString %>" 
            
        SelectCommand="SELECT [AwardTypes], [UniqueID] FROM [Recognition_AwardTypes] ORDER BY [AwardTypes]" 
        UpdateCommand="UPDATE Recognition_AwardTypes SET AwardTypes = @AwardTypes WHERE (UniqueID = @UniqueID)" 
        DeleteCommand="Delete from Recognition_AwardTypes WHERE (UniqueID = @UniqueID)">

            <DeleteParameters>
                <asp:Parameter Name="UniqueID" />
            </DeleteParameters>

            <UpdateParameters>
                <asp:Parameter Name="AwardTypes" />
                <asp:Parameter Name="UniqueID" />
            </UpdateParameters>
        </asp:SqlDataSource>
 
 </asp:Content>


DougP
 
You can do it that way, but I strongly suggest you don't. The last thing you want to do is use any of the datasource controls. They will work if you are doing something simple. Once you need to do anything slightly complicated, they will fail. Also, there is no way to debug them. What happens if your results are not as expected? Normally, you would debug your code and SQL. You can't do that with the datasource controls. They are a black box.

If this works for you then great. But if you need to modify it,m you will probably run into problems, then you will post back here with questions on it. And it be related to the datasource control.

I have already given you the answer in a previous post. Create all the columns manually, using template columns. Then use FindControl() in the code behind. It WILL work, I do it all the time.

Good luck. Hopefully you will not have any issues with this page.
 
jbenson001, this one is as simple as it gets. just changing/deleing one column in the grid/table.

Thanks again for all your help in this forum.

[wink]

DougP
 
Glad to help, that's why I'm here.. :)

You should try my suggestion when you get some time. You will see how easy it is. And more importantly, useful and flexible.

 
I have never gotten it to work until now. So to me it is anything but easy. I have a lot of experince in a lot of things as evident of my profile here over the last 14 years. I am astounded at how much stuff I got involved in, Including, making a Barcode forum way back.

I need an sxample for .NET 4.0. There are tons of examples but none of them seem to work when I paste it in my code.
I created a Time Tracking app last year and could never get anything to work, so I had to put a whole separate screen to go to, then edit and come back.

I do this 40 hours a week, so it's not like I am not trying.

DougP
 
didn't say you weren't trying.. just was saying that if you had the time(which is hard to come by), to try my example. I say that because I use that way of doing it all the time. It has many advantages. This way, in the future, you will not pull your hair out trying to get something that should be easy to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top