Gridview RowEdit and DataBinding without SqlDataSource

Last post 08-29-2008 2:38 PM by dotnetkode. 3 replies.

Sort Posts:

  • Gridview RowEdit and DataBinding without SqlDataSource

    08-29-2008, 11:26 AM
    • Member
      99 point Member
    • adarwich
    • Member since 02-27-2003, 12:24 AM
    • Posts 90

    Hi there,

    I have a gridview (gvList), that I would like to populate programatically from a class and then use the rowedit features. Is this possible, or do I need to create some type of custom control? Currently, I've used a SqlDataSource for testing purposes. For example:

    <asp:GridView ID="gvList" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ID" AllowSorting="true" AllowPaging="false" OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating" ShowFooter="true">
    <Columns>
        <asp:BoundField HeaderText="Volume" DataField="StartVolume" SortExpression="StartVolume" />
    </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" UpdateCommand="UPDATE tbl_JournalVolumes SET StartVolume = @StartVolume WHERE ID = @ID" ConnectionString="<%$ ConnectionStrings:DB_1 %>" ></asp:SqlDataSource>

     C#

    if (this.JournalInfo != null)
    {

    SqlDataSource1.SelectCommand = "SELECT ID, StartVolume FROM tbl_JournalVolumes WHERE tbl_JournalVolumes.JournalID = " + jItm.ID.ToString();

    }

     

    I would like to instead BindData using this, or similar:

    CMSLibrary.JournalVolumesDB jvDB = new CMSLibrary.JournalVolumesDB();
    CMSLibrary.JournalVolumesInfo[] jvAll = jvDB.JournalVolumes_GetAllByJournalIDIgnoreStatus(this.JournalInfo.ID);

    gvList.DataSource = jvAll;
    gvList.DataBind();

     

    I guess I'm wondering if I can assume jvAll to the SqlDataSource? I also have a class built to update the necessary SQL tables (jvDB.JournalVolumes_EditJournalVolumeSettings). Can this be assigned somehow to the SqlDataSource? Or, can I throw out the SqlDataSource and use some other method to bind and edit data line by line?

    Thanks!

     

  • Re: Gridview RowEdit and DataBinding without SqlDataSource

    08-29-2008, 11:31 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs

    It looks like you should be using an ObjectDataSource as opposed to a SqlDataSource.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: Gridview RowEdit and DataBinding without SqlDataSource

    08-29-2008, 12:10 PM
    • Member
      99 point Member
    • adarwich
    • Member since 02-27-2003, 12:24 AM
    • Posts 90

     Hi Ed,

    Thanks for reply, and you are right. I'm not overly familiar with the ObjectDataSource. I've changed my code to:

     <asp:GridView ID="gvList" runat="server" DataSourceID="objList" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ID" AllowSorting="true" AllowPaging="false" OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating" ShowFooter="true">
    <Columns>
        <asp:BoundField HeaderText="Volume" DataField="StartVolume" SortExpression="StartVolume" />
    </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="objList" SelectMethod="GetData" runat="server" TypeName="CMSLibrary.JournalVolumesInfo"></asp:ObjectDataSource> 

     public CMSLibrary.JournalVolumesInfo[] GetData()
        {
            CMSLibrary.JournalVolumesDB jvDB = new CMSLibrary.JournalVolumesDB();
            CMSLibrary.JournalVolumesInfo[] jvAll = jvDB.JournalVolumes_GetAllByJournalIDIgnoreStatus(this.JournalInfo.ID);

            return jvAll;
        }

     

    but receive the following error

    ObjectDataSource 'objList' could not find a non-generic method 'GetData' that has no parameters. 

     

    What am I missing?

    Thanks!

  • Re: Gridview RowEdit and DataBinding without SqlDataSource

    08-29-2008, 2:38 PM
    • Participant
      766 point Participant
    • dotnetkode
    • Member since 01-22-2008, 4:47 PM
    • Herndon, VA
    • Posts 125

     

    Why don't you have class called Person with three properties.

    then have something like this.

    List<Person> coolPeople=new List<Person>();

     

    then in your while loop

    Person p=new Person();

    //set the person properties

     //add the person to the list

     

    return the list

     

     

     

    ~ Remember To Mark The Posts Which Helped You As The ANSWER ~
Page 1 of 1 (4 items)