注册 登录
编程论坛 ASP.NET技术论坛

如何知道DataGrid的哪一行被选中?

zhzh 发布于 2007-04-27 17:32, 1919 次点击
页面上有一个DG,和一个按扭,绑定数据后,选中需要修改的那行数据
点击按钮,会获取DG中选中那行的ID,然后自动跳到修改页面!
如何获取选中那行的ID?
17 回复
#2
卡洛2007-04-27 17:37

我都是用模板做的。。直接做我没试过。
#3
rstp2007-04-27 18:00

this.DataGrid1.SelectedItem.Cells[0].Text.ToString()
Cells[]代表你的表中为id那一些的序号

#4
zhzh2007-04-27 18:21
摸版列用多了,想换种方式
rstp能具体点吗?好象不行?
#5
yunj11052007-04-27 18:44
这是我做的你参考一下吧
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="#00cc66" BorderColor="#339966" BorderStyle="none" BorderWidth="1px"
CellPadding="1" ForeColor="black" GridLines="Vertical" OnItemCommand="dgitem"
OnPageIndexChanged="datagrid_page" PageSize="9" Width="500">
<SelectedItemStyle BackColor="white" Font-Bold="true" ForeColor="white" />
<AlternatingItemStyle BackColor="#CCFF99" />
<ItemStyle BackColor="White" />
<HeaderStyle BackColor="#99FF99" Font-Bold="True" ForeColor="Black" />
<FooterStyle BackColor="Black" />
<Columns>
<asp:BoundColumn DataField="note_num" Visible=false>
</asp:BoundColumn>
<asp:BoundColumn DataField="note_date" HeaderText="发布日期">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="note_name" HeaderText="公告主题">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="note_content" HeaderText="公告内容">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="buted" runat="server" CommandName="edit" CssClass="button" Text="编辑" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:Button ID="butdel" runat="server" CommandName="delete" CssClass="button" Text="删除" /></ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle BackColor="#99FF99" Mode="NumericPages" />
</asp:DataGrid>
public partial class Note : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCmd = "select * from lab_note";
if (!IsPostBack)
{
Comm1 note_co = new Comm1();
note_co.dg_Bind(strCmd,DataGrid1);
}
}
protected void datagrid_page(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
string strCmd1 = "select * from lab_note";
Comm1 note_pa = new Comm1();
note_pa.dg_Bind(strCmd1, DataGrid1);
}
public void dgitem(object sender, DataGridCommandEventArgs e)
{
int notenum = int.Parse(e.Item.Cells[0].Text);
string notename = e.Item.Cells[2].Text;
if (e.CommandName == "edit")
{
string str = "<script language='javascript' defer>ret=window.showModalDialog('Note_Add.aspx?Action=edit&Note_num=" + notenum + "',window,'dialogHeight:300px;dialogWidth:600px;center:yes;Help:No;Resizable:No;Scroll:auto;Status:No;');</script>";
Response.Write(str);
}
if (e.CommandName == "delete")
{
Response.Write("<script defer>confirm('确定要删除吗?');</script>");

string strCmd2 = "delete lab_note where note_num='" + notenum + "'";
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);
SqlCommand myCommand = new SqlCommand(strCmd2, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();

}
string strCmd3 = "select * from lab_note";
Comm1 note_bu = new Comm1();
note_bu.dg_Bind(strCmd3, DataGrid1);
}
#6
无聊的爱2007-04-27 19:41

#7
无聊的爱2007-04-27 19:50
写的不明白,里面的方法都不知道是什么意思?
#8
rstp2007-04-27 20:31
回复:(zhzh)[em04]摸版列用多了,想换种方式rstp能...
比如说你的datagrid里面填充的数据库中的字段 有一列为 userID 并且是datagrid的第一列
在datagrid的属性生成器里面添加一个 按钮列(选择)同时设置一下选定项的背景色(否则是否被选择你也看不出来)
再建立一个按钮 “编辑”设定一个命令比如edit,在datagrid的事件里面的itemCommand里面双击 生成事件处理程序
在里面写
if(e.CommandName=="edit")
{
string abc = this.DataGrid1.SelectedItem.Cells[0].Text.ToString()
}
如果userID是第二列那么就要Cell[1]了


运行以后,当你选择了某一项后,点击编辑按钮,就可以在abc中获得 id的值。。。你想让edit按钮做什么,就写在这个事件处理之中就可以了

[此贴子已经被作者于2007-4-27 20:32:12编辑过]

#9
zhzh2007-04-27 23:16

我跟楼上的设了一下,DG里面的每一行都选不了了,我本来设了选中时的背景色的!
你建立那个按钮列作用是什么啊?你所说的那个按钮是在DG外面吗?
我的怎么都不行呢?

#10
zhzh2007-04-27 23:33
不是yunj1105那种样式!谢谢关注,呵呵
就是整个页面有一个DG绑定数据了
还有一个"修改"的按纽
当你选中要修改的那行
按"修改",他就能获取你选中行的第一列myID
就是这样
可是我照rstp的做了,不行啊!
#11
zhzh2007-04-28 00:22
if(e.CommandName=="edit")
{
string abc = this.DataGrid1.SelectedItem.Cells[0].Text.ToString()
}

未将对象实例化?!为什么? 
#12
zhzh2007-04-28 00:46

已经解决
谢谢大家

#13
rstp2007-04-28 18:41
恩,解决就好
#14
zhzh2007-04-28 18:58

但是如何设置被选中那一行的背景色啊!就是鼠标点击哪一行就背景色改变
我设了,起先管用,但是commandName一添加就又不管用了

#15
rstp2007-04-28 22:09
你的datagrid的绑定要放在if(!Page.isPostBack)里面。
#16
guoxhvip2007-04-28 22:22

有个CurrentCell可以获得当前焦点的单元格,很多问题需要查阅MSDN

#17
zhzh2007-04-29 01:06
难道简单吗?还是我的人品问题啊,我的DG就是怎么设选中项都没有反应!

1.属性生成器-列-格式试过不行
2.CurrentCell不太明白!?
3.ItemDataBound里添加新属性试过不行
4.弄了一个实在搞不懂的JS倒是可以了,但是实在不想用这种方法!

还有没有了?!!指点一下吧!
我的设置就是很简单的设一下鼠标点击DG中的哪一行,也就是选中行,该行的颜色就高亮一点让人识别
整的我灰心丧气的!


实在搞不明白为什么了.真的鼠标在选中行上怎么点都没有反应!!??


#18
zhzh2007-04-29 01:08
有没有好看一点的DG设置代码,贴出来小弟学习学习吧!跪谢了
1