Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Delete MutiRows with mouse drawing using ASP.NET and C#
Delete MutiRows with mouse drawing using ASP.NET and C#


ASP.NET Controls Tutorial

This tutorial will show you how to delete multi-rows by drawing mouse.

This tutorial will show you how to delete multi-rows by drawing mouse. First, you will need to import the System.Data.SqlClient namespace for binding data to WebDataGrid1.

using System.Data.SqlClient;

Function DataBind() will bind data to WebDataGrid1,and create WebDataGrid1 layout.

private void DataBind()
{
string sql = "select * from ContactInfo";
DataSet ds=GetDataSet(sql);
string[] cols = new string[ds.Tables[0].Columns.Count];
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
cols[i]=ds.Tables[0].Columns[i].ColumnName;
}
string[] width = new string[]{"0","40","15","15","15","15"};
this.WebDataGrid1.ColumnsField = cols;
this.WebDataGrid1.ColumnsWidth = width;
this.WebDataGrid1.GridID = "WebDataGrid1";
this.WebDataGrid1.GridDataSource = ds.Tables[0];
this.WebDataGrid1.GridBind();
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Function DataGrid1_ItemDataBound define mouse even, those even will carry out mouse drawing.

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType ==ListItemType.Header)
{
e.Item.Attributes.Add("style","BACKGROUND-IMAGE: url('headeImage.jpg')");
e.Item.ID = "Header";
}
else
{
e.Item.Attributes.Add("onclick", "_SelectTheRow(this)");
e.Item.Attributes.Add("onmousedown","_OnMouseDown(this)");
e.Item.Attributes.Add("onmouseup","_OnMouseUp(this)");
e.Item.Attributes.Add("onmouseover","_OnMouseOver(this)");
e.Item.Attributes.Add("onmouseout","_OnMouseOut(this)");
e.Item.Style.Add("background-color","#ffffff");
}
e.Item.Cells[0].Style.Add("display","none");
}

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

We use the btnDelete event to do multi-rows delete.  Mouse drawing will collect all rows information, and pass those information to delete even.

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if (!id.Equals(string.Empty))
{
if (id.EndsWith(","))
{
id = id.Substring(0, id.Length - 1);
}
delete(id);
this.Response.Redirect(Request.Url.ToString(), true);
}
else
{
this.Response.Redirect(Request.Url.ToString(), true);
}
}

private void delete(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sql= @"declare @sql nvarchar(400)
set @sql = 'delete from ContactInfo where ID in('+@ID+')'
exec( @sql)";
SqlCommand comm=new SqlCommand(sql,conn);
SqlParameter parm1=new SqlParameter("@ID",SqlDbType.VarChar,20);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataBind();
}
#endregion

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if(id.EndsWith(","))
{
id=id.Substring(0,id.Length-1);
}
delete(id);
this.Response.Redirect( Request.Url.ToString(),true );
}

The front end DatagridheadersortCsharp.aspx page looks something like this:

<asp:Button id="btnDelete" style="Z-INDEX: 102; LEFT: 11px; POSITION: absolute; TOP: 206px" runat="server" Text="Delete" onclick="btnDelete_Click"></asp:Button>
<br />
<br />
<br />
<br />
<uc1:WebDataGrid id="WebDataGrid1" runat="server"></uc1:WebDataGrid>
</fieldset>

<SCRIPT type="text/javascript"><!--
function _GoToPageDetail()
{
var childs = document.getElementById("WebDataGrid1_DataGrid1").rows;
var url = "";
for(var i=1;i<childs.length;i++)
{
var child = childs[i];
if(child.style.backgroundColor=='lavender')
{
var tds = child.children;
url = "detatil.aspx?id="+tds[0].innerText;
break;
}
}
if(url!="")
{
window.location.href = url;
}
else if(childs.length>1)
{
var child = childs[1];
var tds = child.children;
window.location.href ="detatil.aspx?id="+tds[0].innerText;
}
}

function Delete()
{
SelectDeleteRows();
var hid = document.getElementById("WebDataGrid1_hiddenDelete");
if(hid=="")
alert('You need to select a row in the list before selecting Delete.\r\nPlease select a row and try again.');
else if(window.confirm("Are you sure to delete all these?"))
{
document.getElementById("btnDelete").click();
}
else
{
return false;
}
}

function _DoubleTheRow(obj)
{
var childs = obj.children;
var td1 = childs[0];
var txtID = td1.innerText;
window.location.href ="detatil.aspx?id"+txtID
}
--></SCRIPT>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DatagridMuriDelCsharp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
this.btnDelete.Attributes.Add("onclick","return Delete();");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

/
private void InitializeComponent()
{

}
#endregion

#region DataBind
private void DataBind()
{
string sql = "select * from ContactInfo";
DataSet ds=GetDataSet(sql);
string[] cols = new string[ds.Tables[0].Columns.Count];
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
cols[i]=ds.Tables[0].Columns[i].ColumnName;
}
string[] width = new string[]{"0","40","15","15","15","15"};
this.WebDataGrid1.ColumnsField = cols;
this.WebDataGrid1.ColumnsWidth = width;
this.WebDataGrid1.GridID = "WebDataGrid1";
this.WebDataGrid1.GridDataSource = ds.Tables[0];
this.WebDataGrid1.GridBind();
}
#endregion

#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion

#region delete
private void delete(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
string sql= @"declare @sql nvarchar(400)
set @sql = 'delete from ContactInfo where ID in('+@ID+')'
exec( @sql)";
SqlCommand comm=new SqlCommand(sql,conn);
SqlParameter parm1=new SqlParameter("@ID",SqlDbType.VarChar,20);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataBind();
}
#endregion

protected void btnDelete_Click(object sender, System.EventArgs e)
{
string id=this.WebDataGrid1.GridSelectItems;//.Split(',');
if(id.EndsWith(","))
{
id=id.Substring(0,id.Length-1);
}
delete(id);
this.Response.Redirect( Request.Url.ToString(),true );
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks