在gridview中仅编辑单行 [英] Edit only single row in gridview
问题描述
大家好,
我正在从事创建联系书的项目.我从Active Directory提取数据并将其放入sql server.但是活动目录中的大多数信息是不完整的,因此我希望允许用户在浏览至联系人时编辑其信息.
我正在使用网格视图显示SQL Server表中的列表.
当我使列表可编辑时,默认情况下它将所有行都设为可编辑".
我试图使用用户Windows身份验证( Users.Identity.Name ),以便仅登录用户行应可编辑.
我是这一切的新手.因此,有人可以指导我如何实现这一目标,或者是否有其他方法或另一种方法可以更好地解决这种情况.
谢谢
Parth
Hi All ,
I am working on project of creating contact book. I am fetching data from Active directory putting it into sql server. But most of information in active directory is incomplete so i want to allow user to edit their info when they browse to contacts.
I am using grid view to display list from my sql server table.
When i am making list editable, by default it make all row "EDITable".
I am trying to use users windows authentication(Users.Identity.Name) so that only logon user row should be editable.
I am new to all this. so can some one guide me how I can achieve this or is there any other way or another control better for this situation.
thanks
Parth
推荐答案
我正在发送刚刚尝试过的小代码段!
希望它足够好!
I am sending small code snippet which I have tried just now!
Hope it is fine enough!
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("one");
DataRow dr=dt.NewRow();
dr["one"] = "not editable";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["one"] = "editable";
dt.Rows.Add(dr1);
dataGridView1.DataSource = dt;
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1["one", e.RowIndex].Value == "not editable")
dataGridView1.Rows[e.RowIndex].ReadOnly = true;
else
dataGridView1.Rows[e.RowIndex].ReadOnly = false;
}
祝您编程愉快.:)
Happy coding.:)
个人不喜欢对网格视图进行就地编辑,因此始终允许用户为此目的导航到专用页面.使用RowDataBound
事件获取用户ID(每行),并将其设置为编辑按钮的命令参数.OnClick
将选定的ID设置到会话中,然后导航到编辑页面.
Personally don''t like in place editing for gridviews so always allow the user to navigate to a dedicated page for the purpose. Use theRowDataBound
event to get the user id (per row) and set those as a command argument of a an edit button. TheOnClick
sets the selected id into the session and then navigates to the edit page.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = e.Row.Cells[0].Text;
// Handle the buttons.
LinkButton button;
// Find the command and add commandarguments.
button = (LinkButton)e.Row.FindControl("LinkButton1");
button.CommandArgument = id;
}
}
和OnClick
事件...
and the OnClick
event...
protected void EditItem(object sender, CommandEventArgs e)
{
Session.Add("UserID", e.CommandArgument);
Response.Redirect("Path_To_Edit_Page");
}
请注意,这只是解决此问题的众多方法之一:在选择最适合您的特定需求之前,请尽可能多地研究.
Note that this is just one of many ways to approach this: look at as many as you can before selecting the most appropriate for your specific needs.
这篇关于在gridview中仅编辑单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!