读书人

直接在datagrid上操作数据解决办法

发布时间: 2012-01-26 19:40:46 作者: rapoo

直接在datagrid上操作数据
想实现datagrid里某个单元格,点击右键跳出查询,修改,删除等...
如果选择查询则跳出一个对应该字段为查询条件的窗口,实现查询功能..
分不多,请达人附个源码.

[解决办法]
很好实现吧!
DataGridView_Click()
{
if (e.button==button.right)
{
if(datagridview.columnsindex==2)
{
menu.show();
}
}

menu_click()
{
switch(type)
{
case "delete ":
....
break;
case "insert ":
....

}
}
}


最后用sqldataadapter.update(dt)更新数据

[解决办法]
<%@ Page language= "c# " Codebehind= "WebForm1.aspx.cs " AutoEventWireup= "false " Inherits= "shiyan.WebForm1 " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> WebForm1 </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
<script type= "text/javascript ">
function oClick(layer)
{
if(event.button==2)
{
var e = document.getElementById( "menu ");
e.style.left = event.clientX;
e.style.top = event.clientY;
e.style.visibility = "visible ";

var i = layer.id.indexOf( "td_ ");
var j = layer.id.indexOf( "- ");
document.getElementById( "put_orderID ").value = layer.id.substr(i+3,j-i-3);
document.getElementById( "put_col ").value = layer.id.substr(j+1);
}
}
function over(layer)
{
layer.style.backgroundColor= "#00ccff ";
}
function out(layer)
{
layer.style.backgroundColor= "#ffffff ";
}
</script>
</HEAD>
<body>
<form id= "Form1 " method= "post " runat= "server ">
<table id= "menu " cellpadding= "2 " cellspacing= "0 " bgcolor=#ffffff oncontextmenu= "self.event.returnValue=false " onmousedown= "if(event.button==1)menu.style.visibility= 'hidden '; " style= "cursor: pointer; Z-INDEX: 2; WIDTH: 100px; POSITION: absolute; visibility: hidden; font-size:13px; BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; BORDER-BOTTOM: #cccccc 1px solid ">
<tr>
<td onmouseover= "over(this) " onmouseout= "out(this) "> 查询 </td>
</tr>
<tr>
<td onmouseover= "over(this) " onmouseout= "out(this) "> 修改 </td>
</tr>
<tr>
<td onmouseover= "over(this) " onmouseout= "out(this) "> 删除 </td>
</tr>
</table>
<div oncontextmenu= "self.event.returnValue=false " onmousedown= "if(event.button==1)menu.style.visibility= 'hidden '; ">
<asp:DataGrid id= "DataGrid1 " runat= "server "> </asp:DataGrid>
</div>
<input id=put_orderID type=text> <input id=put_col type=text>
</form>
</body>
</HTML>


================================================
//添加命名空间 System.Data.SqlClient;

protected static DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
SqlConnection conn = new SqlConnection( "server=.;database=Northwind;uid=sa;pwd=; ");
SqlDataAdapter da = new SqlDataAdapter( "select top 8 OrderID,CustomerID,OrderDate from Orders ",conn);
ds = new DataSet();
da.Fill(ds);
this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
this.DataGrid1.DataKeyField = "OrderID ";
this.DataGrid1.DataBind();
}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
for(int i=0;i <e.Item.Cells.Count;i++)
{
e.Item.Cells[i].ID = "td_ "+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()+ "- "+ds.Tables[0].Columns[i].ToString();
e.Item.Cells[i].Attributes.Add( "onmouseup ", "oClick(this) ");
e.Item.Cells[i].Style.Add( "cursor ", "pointer ");
}
}
}

读书人网 >C#

热点推荐