473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlDataSource and GridView

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 ***
May 31 '06 #1
1 7869
You can always use a TemplateColumn and put your own Button with a OnClientClick
property where you use a prompt(). When the event makes it back to the server,
to have the Button cause the GridView and thus the SqlDataSource to trigger
the DeleteCommand the CommandName on the Button must be the well-known value
"Delete".

-Brock
http://staff.develop.com/ballen

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 ***

Jun 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5025
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my select, remember it, and then use it in the update. It works just fine when I have full control of the whole process. I want to do the same for...
3
2221
by: adam222 | last post by:
hello, i have a web-form with a GridView control, i wanted to update & delete, using the AutoGenerateEditButton. when i used it with sqlDataSource (executing SP in the DB) it works like a charm, but when i want to pass the data through the application's layers (BL etc.), it did not work. it seems that the 'AutoGenerateEditButton' is...
3
12727
by: Dorte | last post by:
Hi, Could someone help me with a couple of links to SqlDatasource documentation on how to use the Gridview and SqlDatasource components in code behind? Basically I'm missing some documentation on how to handle different tasks programmatically such as selects, updates, sorting of data etc. in code behind if I prefer to set all Gridview and...
3
4520
by: simonZ | last post by:
In gridView I have dropdown list : <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField...
2
14404
by: phil | last post by:
I have an ASP.NET application with an SQLdataSource and a Gridview Everything works very well except I have a 'Button' that goes off and changes some of the record data back in the underlying database records. When I press this the data is definitely changed in the database - but on screen nothing seems to happen. So how do I force the...
0
2134
by: LiamLiamLiam | last post by:
G'day all. I having a problem with my formview. I'll ty to explain my situation as best as i can. I have a page with a search field at the top which is just a simple asp:textbox. Below that i have a gridview which displays the results of the search. if no search is entered, then the gridview shows all records. Below the gridview i have a...
1
2527
by: Vili | last post by:
Hi all I would like to get some ideas and opinions of my solution for dataconnection on asp.net 2.0 I have created a class which encapsulates the sqldatasource usage. With my class one can set select-, insert-, update- and deletecommand with and without parameters and also get output parameters
4
9775
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This sounds easy but the next requirement is that the GridView is editable. So I have included the SelectCommand and the UpdateCommand on the...
1
1159
by: sweatha | last post by:
Hi I connected SQLDataSource in GridView by using coding <asp:GridView ID="Search_GridView" runat="server" Style="z-index: 100; left: 2px; position: absolute; top: 270px" AutoGenerateColumns="False" OnSelectedIndexChanged="Search_GridView_SelectedIndexChanged" CellPadding="20" Width="720px" AllowPaging="True"...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.