for(int i=0;i< datagridrowscount;i++)
if(datagridrowscell[i][M]tostringequals(查询比较对象)) //M是要查询的第M+1列
{
return i;
也可以return datagridrowscells[i][K]--这个是本行的关键标示,能够确定一条记录
i=gridviewrowscount;
}
更新表中的数据
DataRow类为了在编辑数据时挂起和恢复数据行的状态提供了3个方法:BeginEdit、EndEdit和CancelEdit方法。当编辑数据时,调用BeginEdit可以挂起任何事件(或异常),使用Items集合指定要修改的数据的列名和新值;使用EndEdit重新恢复任何事件(或异常);使用CancelEdit则可以回滚任何更改和重新激活任何事件或异常。
1 //获取Employees表中的第4行数据
2 DataRow drEmployee = dtEmployeesRows[3];
3
4 drEmployeeBeginEdit();
5
6 drEmployee("EmpName") = "John";
7 drEmployee("Title")="Sales";
8 drEmployeeEndEdit();
没看明白可以查看参考资料
为什么要删除啊?你在每个行里添加一个状态,便是该行有没有修改,如果修改了根据该行的ID直接修改数据库就可以了,不用删除数据库里的行
没有数据库 *** 作啊,那就可以根据行号直接修改或者删除指定的行啊
DataRow[] dr = dtSelect("ID='"+ id +"'");
foreach (DataRow fr in dr)
{
dtRowsRemove(fr);
}
如果是主键,你可以到GridView里面去设定成如:
DataKeyNames="id"
<asp:GridView ID="gdvUserInfo" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
获取的时候:
/// <summary>
/// 删除信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfoDataKeys[eRowIndex]ValueToString();
if (infoDeleteUserInfoById(id) > 0)
{
thislblMessageText = "删除成功!";
LoadInfo();
}
else
{
thislblMessageText = "删除失败!";
LoadInfo();
}
}
/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfoEditIndex = eNewEditIndex;
LoadInfo();
}
/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfoEditIndex = -1;
LoadInfo();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfoDataKeys[eRowIndex]ValueToString();
TextBox txtUserId = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfoRows[eRowIndex]Cells[0]FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = infoGetUserInfoById(userId);
userUserName = txtUserNameTextToString();
userPassword = txtPassWordTextToString();
if (infoModifyUserInfo(user) > 0)
{
thislblMessageText = "修改成功!";
gdvUserInfoEditIndex = -1;
LoadInfo();
}
else
{
thislblMessageText = "修改失败!";
gdvUserInfoEditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{
thislblMessageText = "修改失败!";
gdvUserInfoEditIndex = -1;
LoadInfo();
}
}
}
/// <summary>
///
/// 光棒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gdvUserInfoRowsCount; i++)
{
if (gdvUserInfoRows[i]RowType == DataControlRowTypeDataRow)
{
gdvUserInfoRows[i]AttributesAdd("onmouseover", "c=thisstylebackgroundColor;thisstylebackgroundColor='#9cf'");
gdvUserInfoRows[i]AttributesAdd("onmouseout", "thisstylebackgroundColor=c");
}
}
}
用的是aspnet,GridView实现它的RowDataBound事件,具体看代码,仅供参考:
前台:
<html xmlns="
<head runat="server">
<title></title>
<!--点击GridView任意行的位置,获取该行的ID值-->
<script type="text/javascript">
function getId(row_index) {
var grid_view = documentgetElementById('<%=GridView1ClientID %>');
var rows = grid_viewrows;
var personID = rows[row_index]cells[0]innerHTML;
alert("获取的ID为:" + personID);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Name" DataField="Name" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Tel" DataField="Tel" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台代码:
public partial class WebForm1 : SystemWebUIPage
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
listAdd(new Person("001", "张三"));
listAdd(new Person("002", "李四"));
listAdd(new Person("003", "王五"));
listAdd(new Person("004", "赵六"));
listAdd(new Person("005", "何七"));
GridView1DataSource = list;
GridView1DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (eRowRowType == DataControlRowTypeDataRow)
{
int row_index = eRowRowIndex + 1;
eRowAttributesAdd("onclick", "getId(" + row_index + ");");
}
}
}
class Person
{
public Person(string id, string name)
{
ID = id;
Name = name;
}
private string id;
public string ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string tel;
public string Tel
{
get { return tel; }
set { tel = value; }
}
}
以上就是关于查询 DataGrid 中某列数据 ,并返回查询结果的行索引全部的内容,包括:查询 DataGrid 中某列数据 ,并返回查询结果的行索引、C#编程中如何改变DataTable里的某一条记录、C# --WindowCE开发 datagrid问题求教!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)