473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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: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 ***
May 31 '06 #1
1 7856
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: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 ***

Jun 1 '06 #2

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

Similar topics

8
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...
3
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...
3
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...
3
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...
2
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...
0
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...
1
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...
4
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...
1
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"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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,...

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.