会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
gaoxiang
专注于.NET技术
博客园
首页
新随笔
联系
订阅
管理
公告
实现DataGrid拖动和排序
1.html
<
HTML
>
<
HEAD
>
<
title
>
MoveDataGrid
</
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"
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
asp:DataGrid
id
="DataGrid1"
style
="BEHAVIOR: url(movegrid.htc)"
runat
="server"
BackColor
="White"
BorderWidth
="1px"
BorderStyle
="None"
BorderColor
="#CC9966"
CellPadding
="4"
Font-Size
="9pt"
>
<
SelectedItemStyle
Font-Bold
="True"
ForeColor
="#663399"
BackColor
="#FFCC66"
></
SelectedItemStyle
>
<
ItemStyle
ForeColor
="#330099"
BackColor
="White"
></
ItemStyle
>
<
HeaderStyle
Font-Bold
="True"
ForeColor
="#FFFFCC"
BackColor
="#990000"
></
HeaderStyle
>
<
FooterStyle
ForeColor
="#330099"
BackColor
="#FFFFCC"
></
FooterStyle
>
<
PagerStyle
HorizontalAlign
="Center"
ForeColor
="#330099"
BackColor
="#FFFFCC"
></
PagerStyle
>
</
asp:DataGrid
>
</
form
>
</
body
>
</
HTML
>
2.cs
public
class
MoveDataGrid : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.DataGrid DataGrid1;
private
string
constring
=
""
;
private
int
nColumn
=
0
;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
string
sql
=
"
select * from testgrid
"
;
DataSet ds
=
GetDataSet(sql);
DataGrid1.Attributes.Add(
"
dragcolor
"
,
"
gray
"
);
DataGrid1.Attributes.Add(
"
slcolor
"
,
"
#ffffcc
"
);
DataGrid1.Attributes.Add(
"
hlcolor
"
,
"
#BEC5DE
"
);
nColumn
=
ds.Tables[
0
].Columns.Count;
this
.DataGrid1.DataSource
=
ds;
this
.DataGrid1.DataBind();
}
GetDataSet
#region
GetDataSet
private
DataSet GetDataSet(
string
sql)
{
constring
=
System.Configuration.ConfigurationSettings.AppSettings[
"
ConnectionString
"
];
SqlDataAdapter sda
=
new
SqlDataAdapter(sql,constring);
DataSet ds
=
new
DataSet();
sda.Fill(ds);
return
ds;
}
#endregion
Web Form Designer generated code
#region
Web Form Designer generated code
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base
.OnInit(e);
this
.DataGrid1.ItemCreated
+=
new
DataGridItemEventHandler(DataGrid1_ItemCreated);
}
/**/
///
<summary>
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
</summary>
private
void
InitializeComponent()
{
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
DataGrid1_ItemCreated(
object
sender, DataGridItemEventArgs e)
{
if
(e.Item.ItemType
==
ListItemType.Header)
{
for
(
int
i
=
0
;i
<
nColumn;i
++
)
{
e.Item.Cells[i].Attributes.Add(
"
Width
"
,
"
200
"
);
}
}
}
}
3.源代码下载
/Files/singlepine/moveandsort.rar
posted on 2005-11-25 11:45
小山
阅读(595)
评论(3)
编辑
收藏
收藏至365Key
所属分类:
DataGrid
Feedback
#
re: 实现DataGrid拖动和排序
2005-12-10 03:34
dddlincsdn
小山,你的这个正好满足偶的需要。
排序行了。
但是拖动不行。拖动的那列就停留在边上,不知为何。
回复
#
re: 实现DataGrid拖动和排序
2005-12-10 10:31
小山
拖动的时候,最后放的位置一定要在datagrid表头部位才可以
回复
#
re: 实现DataGrid拖动和排序
2005-12-11 01:21
dddlincsdn
还是不中,始终显示在最左边,且只能上下移动。
回复
posted on
2006-03-10 10:22
S孤单一吻S
阅读(
869
) 评论(
1
)
收藏
举报
刷新页面
返回顶部
博客园
© 2004-2025
浙公网安备 33010602011771号
浙ICP备2021040463号-3
Feedback
排序行了。
但是拖动不行。拖动的那列就停留在边上,不知为何。 回复