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:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$
ConnectionStrin gs:XeroxConnect ionString %>"
SelectCommand=" ViewForecast"
SelectCommandTy pe="StoredProce dure">
</asp:SqlDataSour ce>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="S qlDataSource1"
SkinID="Grey" AutoGenerateCol umns="false"
DataKeyNames="F orecastKey" AllowSorting="t rue"
OnRowDataBound= "GridView1_RowD ataBound"
OnRowCommand="G ridView1_RowCom mand">
<Columns>
<asp:TemplateFi eld HeaderText="Com pany"
SortExpression= "Name">
<ItemTemplate >
<asp:Label
ID="lblCompany " Text='<%# Eval("Name") %>' runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="For ecast Type" SortExpression= "ForecastDescri ption">
<ItemTemplate >
<asp:Label
ID="lblForecast Type" Text='<%# Eval("ForecastD escription") %>'
runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Val ue (£)" SortExpression= "MoneyValue ">
<ItemTemplate >
<asp:Label
ID="lblMoneyVal ue" Text='<%# Eval("MoneyValu e", "{0:#,###.0 0}") %>'
runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Pro bability (%)" SortExpression= "Probabilit y">
<ItemTemplate >
<asp:Label
ID="lblProbabil ity" Text='<%# Eval("Probabili ty") %>'
runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Wee ks 1-3 (£) x (%)" SortExpression= "ThreeWeekPerce ntage">
<ItemTemplate >
<asp:Label
ID="lblThreeWee kPercentage" runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Wee k 4 (£) x (%-25%)" SortExpression= "FourthWeekPerc entage">
<ItemTemplate >
<asp:Label
ID="lblFourthWe ekPercentage" runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Due Date" SortExpression= "InvoiceDat e">
<ItemTemplate >
<asp:Label
ID="lblDueDate " Text='<%# Eval("InvoiceDa te", "{0:dd/MM/yyyy}") %>'
runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld
HeaderText="Las t Edit Date" SortExpression= "CreateDate ">
<ItemTemplate >
<asp:Label
ID="lblLastEdit Date" Text='<%# Eval("CreateDat e", "{0:dd/MM/yyyy}") %>'
runat="server"> </asp:Label>
</ItemTemplate>
<ItemStyle Height="24px"
Width="50px" />
</asp:TemplateFie ld>
<asp:CommandFie ld
ShowEditButton= "True" ButtonType="Lin k" ShowCancelButto n="True"
UpdateText="Upd ate"
EditText="Edit" CancelText="Can cel" />
<asp:ButtonFiel d
ButtonType="Lin k" CommandName="De lete" Text="Delete" />
</Columns>
</asp:GridView>
protected void GridView1_RowDa taBound(object sender,
GridViewRowEven tArgs e)
{
if (e.Row.RowType == DataControlRowT ype.DataRow)
{
Label ForecastType =
(Label)e.Row.Fi ndControl("lblF orecastType");
switch (ForecastType.T ext)
{
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.Fi ndControl("lblP robability");
switch (Probability.Te xt)
{
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.R ow.Cells[8].Controls[0];
EditButton.Fore Color = System.Drawing. Color.Blue;
LinkButton DeleteButton =
(LinkButton)e.R ow.Cells[9].Controls[0];
DeleteButton.Fo reColor = System.Drawing. Color.Blue;
DeleteButton.At tributes.Add("o nclick", "javascript:ret urn "
+
"confirm('A re you sure you want to delete forecast record "
+
DataBinder.Eval (e.Row.DataItem , "ForecastKe y") + "?')");
}
}
protected void GridView1_RowCo mmand(object sender,
GridViewCommand EventArgs e)
{
if (e.CommandName == "Delete")
{
// 1)Convert the row index stored in the CommandArgument
property to an Integer
int index = Convert.ToInt32 (e.CommandArgum ent);
// 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.DataItemInd ex].Value;
//2) delete row
DataAccess da = new DataAccess();
// delete row etc
// GridView1.EditI ndex = -1;
// //refresh gridview
// GridView1.DataB ind();
}
*** Sent via Developersdex http://www.developersdex.com ***