SqlDataSource and GridView

Discussion in 'ASP .Net' started by Mike P, May 31, 2006.

  1. Mike P

    Mike P Guest

    When you use a SqlDataSource to hook up data to a GridView it seems to
    be to be very inflexible. For example, I want to create my own Delete
    button with my own code and I also want to create a popup warning for
    the user before delete takes place. Whenever I do this with a
    SqlDataSource, I get the error 'Deleting is not supported by data source
    'SqlDataSource1' unless DeleteCommand is specified.' Which means I have
    to specify a sproc or text to use as the Delete and I can't use my popup
    etc. Can anybody tell me if there is a way around this? Here is my
    code :

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$
    ConnectionStrings:XeroxConnectionString %>"
    SelectCommand="ViewForecast"
    SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>

    <asp:GridView ID="GridView1" runat="server"
    DataSourceID="SqlDataSource1"
    SkinID="Grey" AutoGenerateColumns="false"
    DataKeyNames="ForecastKey" AllowSorting="true"
    OnRowDataBound="GridView1_RowDataBound"
    OnRowCommand="GridView1_RowCommand">
    <Columns>
    <asp:TemplateField HeaderText="Company"
    SortExpression="Name">
    <ItemTemplate>
    <asp:Label
    ID="lblCompany" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Forecast Type" SortExpression="ForecastDescription">
    <ItemTemplate>
    <asp:Label
    ID="lblForecastType" Text='<%# Eval("ForecastDescription") %>'
    runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Value (£)" SortExpression="MoneyValue">
    <ItemTemplate>
    <asp:Label
    ID="lblMoneyValue" Text='<%# Eval("MoneyValue", "{0:#,###.00}") %>'
    runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Probability (%)" SortExpression="Probability">
    <ItemTemplate>
    <asp:Label
    ID="lblProbability" Text='<%# Eval("Probability") %>'
    runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Weeks 1-3 (£) x (%)" SortExpression="ThreeWeekPercentage">
    <ItemTemplate>
    <asp:Label
    ID="lblThreeWeekPercentage" runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Week 4 (£) x (%-25%)" SortExpression="FourthWeekPercentage">
    <ItemTemplate>
    <asp:Label
    ID="lblFourthWeekPercentage" runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Due Date" SortExpression="InvoiceDate">
    <ItemTemplate>
    <asp:Label
    ID="lblDueDate" Text='<%# Eval("InvoiceDate", "{0:dd/MM/yyyy}") %>'
    runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:TemplateField
    HeaderText="Last Edit Date" SortExpression="CreateDate">
    <ItemTemplate>
    <asp:Label
    ID="lblLastEditDate" Text='<%# Eval("CreateDate", "{0:dd/MM/yyyy}") %>'
    runat="server"></asp:Label>
    </ItemTemplate>
    <ItemStyle Height="24px"
    Width="50px" />
    </asp:TemplateField>

    <asp:CommandField
    ShowEditButton="True" ButtonType="Link" ShowCancelButton="True"
    UpdateText="Update"
    EditText="Edit" CancelText="Cancel" />

    <asp:ButtonField
    ButtonType="Link" CommandName="Delete" Text="Delete" />
    </Columns>
    </asp:GridView>

    protected void GridView1_RowDataBound(object sender,
    GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Label ForecastType =
    (Label)e.Row.FindControl("lblForecastType");

    switch (ForecastType.Text)
    {
    case "Analysis":
    e.Row.Cells[1].BackColor =
    System.Drawing.Color.Cyan;
    break;

    case "Call Centre":
    e.Row.Cells[1].BackColor =
    System.Drawing.Color.Lime;
    break;

    case "Data":
    e.Row.Cells[1].BackColor =
    System.Drawing.Color.Yellow;
    break;

    case "Data/Analysis":
    e.Row.Cells[1].BackColor =
    System.Drawing.Color.Magenta;
    break;
    }

    Label Probability =
    (Label)e.Row.FindControl("lblProbability");

    switch (Probability.Text)
    {
    case "0.25":
    e.Row.Cells[3].Text = "C (25%)";
    e.Row.Cells[3].BackColor =
    System.Drawing.Color.Coral;
    break;

    case "0.5":
    e.Row.Cells[3].Text = "B (50%)";
    e.Row.Cells[3].BackColor =
    System.Drawing.Color.OrangeRed;
    break;

    case "0.75":
    e.Row.Cells[3].Text = "A (75%)";
    e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
    break;

    case "0.751":
    e.Row.Cells[3].Text = "Proforma (75%)";
    e.Row.Cells[3].BackColor =
    System.Drawing.Color.Maroon;
    break;

    case "1":
    e.Row.Cells[3].Text = "In (100%)";
    e.Row.Cells[3].BackColor =
    System.Drawing.Color.White;
    break;
    }

    LinkButton EditButton =
    (LinkButton)e.Row.Cells[8].Controls[0];
    EditButton.ForeColor = System.Drawing.Color.Blue;

    LinkButton DeleteButton =
    (LinkButton)e.Row.Cells[9].Controls[0];
    DeleteButton.ForeColor = System.Drawing.Color.Blue;

    DeleteButton.Attributes.Add("onclick", "javascript:return "
    +
    "confirm('Are you sure you want to delete forecast record "
    +
    DataBinder.Eval(e.Row.DataItem, "ForecastKey") + "?')");
    }
    }

    protected void GridView1_RowCommand(object sender,
    GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Delete")
    {
    // 1)Convert the row index stored in the CommandArgument
    property to an Integer
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button clicked by the
    user from the Rows collection
    GridViewRow row = GridView1.Rows[index];

    //get datakeys
    int id = (int)GridView1.DataKeys[row.DataItemIndex].Value;

    //2) delete row
    DataAccess da = new DataAccess();

    // delete row etc



    // GridView1.EditIndex = -1;

    // //refresh gridview
    // GridView1.DataBind();
    }




    *** Sent via Developersdex http://www.developersdex.com ***
    Mike P, May 31, 2006
    #1
    1. Advertising

  2. Mike P

    cyp7

    Joined:
    Oct 25, 2006
    Messages:
    6
    'Deleting is not supported by data source

    Only joined this group recently, so you've probably figured this out by now, but what you need to do is go back to where you Configure your data source, and when you select the Table you want using the Wizard, select "Advanced" and check off the box to create Insert, Update, and Delete statements. That'll solve the problem.

    Carolyn
    cyp7, Nov 3, 2006
    #2
    1. Advertising

  3. Mike P

    kshashank

    Joined:
    Dec 27, 2010
    Messages:
    1
    Of course this is not intended as a reply to original poster but just saying it here just in case someone is looking for it...you can use a template column let say with a delete image and make sure there is command name called "Delete" specified there. If "Delete" command name is specified then ASP.NET takes over the processing and your code behind will not even called.
    kshashank, Dec 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UA==?=
    Replies:
    1
    Views:
    2,199
    Michelle Hlaing
    Nov 30, 2004
  2. Henry Habermacher [MVP Access]

    GridView - Changing FilterExpression of SQLDataSource and Paging

    Henry Habermacher [MVP Access], Jan 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    10,982
    kmarchiony
    Jul 31, 2007
  3. =?Utf-8?B?RGFiYmxlcg==?=

    insert row using Gridview and SqlDataSource?

    =?Utf-8?B?RGFiYmxlcg==?=, Mar 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    10,644
    =?Utf-8?B?RGFiYmxlcg==?=
    Mar 20, 2006
  4. =?Utf-8?B?RG9ydGU=?=

    Gridview and SqlDatasource from code behind

    =?Utf-8?B?RG9ydGU=?=, Mar 30, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    24,759
    =?Utf-8?B?Y2xpY2tvbg==?=
    Mar 30, 2006
  5. phil
    Replies:
    2
    Views:
    1,100
    Shahzad Godil
    Nov 14, 2006
Loading...

Share This Page