只编辑某行的某列????????????

Nesta_13 2009-03-30 07:21:38
请问大家怎么只编辑某行某列?
就是一个列表,当鼠标点击到某个数据项的时候 比如说第一行的第一列 就可以编辑这条数据项
以此类推 每条数据项都可以编辑

请问这个怎么实现?
...全文
224 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nesta_13 2009-04-03
  • 打赏
  • 举报
回复
非常感谢cwmwss的代码
也感谢大家的帮忙
结贴
lonelySurvive 2009-03-31
  • 打赏
  • 举报
回复
回帖是一种美德!每天回帖即可获得 10 分可用分!
Nesta_13 2009-03-31
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 itcrazyman 的回复:]
datagrid就可以啊,只要某列为可编辑列,其它的不设置就OK了
[/Quote]
我要的并不是某列或则某行可以编辑
而是某个数据项可以编辑
比如点第一行第一列的时候 这个数据项可以编辑 以此类推
itcrazyman 2009-03-31
  • 打赏
  • 举报
回复
datagrid就可以啊,只要某列为可编辑列,其它的不设置就OK了
zzxap 2009-03-31
  • 打赏
  • 举报
回复
GridView.Rows[RowIndex].Cells[ColIndex].tbxQty.value
jwdream2008 2009-03-31
  • 打赏
  • 举报
回复
学习。。。
lrc520 2009-03-31
  • 打赏
  • 举报
回复
up
路人乙e 2009-03-31
  • 打赏
  • 举报
回复
自己做吧,不复杂,麻烦点!
hy_lihuan 2009-03-31
  • 打赏
  • 举报
回复
应该说没有现成的控件,自己做需要花不少的时间
cwmwss 2009-03-31
  • 打赏
  • 举报
回复
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="EditIndividualCell.aspx.cs"
Inherits="Control_EditIndividualCell" Title="在GridView中针对鼠标单击的某一独立单元格进行编辑" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" AllowSorting="True" OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
<asp:DropDownList ID="ddlCountry" runat="server" Visible="False" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT DISTINCT [Country] FROM [Employees]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Country]) VALUES (@LastName, @FirstName, @Country)"
SelectCommand="SELECT [LastName], [FirstName], [Country], [EmployeeID] FROM [Employees]"
UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Country] = @Country WHERE [EmployeeID] = @EmployeeID">
<DeleteParameters>
<asp:Parameter Name="EmployeeID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Message" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
</asp:Content>
cwmwss 2009-03-31
  • 打赏
  • 举报
回复
/*为了方便,我们称Label为显示控件,TextBox或DropDownList为编辑控件*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Control_EditIndividualCell : System.Web.UI.Page
{
private const int _firstEditCellIndex = 2;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataBind();
}

if (this.GridView1.SelectedIndex > -1)
{
// 调用GridView的UpdateRow方法
this.GridView1.UpdateRow(this.GridView1.SelectedIndex, false);
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 从第一个单元格内获得LinkButton控件
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// 返回一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");

// 给每一个可编辑的单元格增加事件
for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// 增加列索引作为事件参数
string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
// 给单元格增加onclick事件
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
// 给单元格增加鼠标经过时指针样式
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;

switch (e.CommandName)
{
case ("SingleClick"):
// 获得行索引
int _rowIndex = int.Parse(e.CommandArgument.ToString());
// 解析事件参数(在RowDataBound中增加的),从而获得被选中的列的索引
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
// 设置GridView被选中的行的索引(每次回发后判断GridView1.SelectedIndex > -1则更新)
_gridView.SelectedIndex = _rowIndex;
// 绑定
_gridView.DataBind();

// 事件记录
this.Message.Text += "单击GridView的行的索引为:" + _rowIndex.ToString()
+ ";列索引为:" + _columnIndex + "<br />";

// 获得被选中单元格的显示控件并设置其不可见
//Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].FindControl("lblLastName");
Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
// 获得被选中单元格的编辑控件并设置其可见
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
// 清除被选中单元格属性以删除click事件
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear();

// 设置焦点到被选中的编辑控件
ClientScript.RegisterStartupScript(GetType(), "SetFocus",
"<script>document.getElementById('" + _editControl.ClientID + "').focus();</script>");
// 如果编辑控件是DropDownList的话,那么把SelectedValue设置为显示控件的值
if (_editControl is DropDownList && _displayControl is Label)
{
((DropDownList)_editControl).SelectedValue = ((Label)_displayControl).Text;
}
// 如果编辑控件是TextBox的话则选中文本框内文本
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}

break;
}
}

/// <summary>
/// 把值(values)从EditItemTemplate转移到NewValues集合里(使用数据源控件的话就需要这步)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView _gridView = (GridView)sender;
string key = "";
string value = "";

// NewValues集合里的key
string[] _columnKeys = new string[] { "LastName", "FirstName", "Country" };

if (e.RowIndex > -1)
{
// 循环每一列
for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
{
// 获得单元格里的控件
Control _displayControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[1];
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3];

// 获得列的key
key = _columnKeys[i - _firstEditCellIndex];

// 如果单元格处于编辑模式的话,那么从编辑控件中获取值
if (_editControl.Visible)
{
if (_editControl is TextBox)
{
value = ((TextBox)_editControl).Text;
}
else if (_editControl is DropDownList)
{
value = ((DropDownList)_editControl).SelectedValue;
}

// 增加key/value对到NewValues集合
e.NewValues.Add(key, value);
}
// 否则从显示控件中获取值
else
{
value = ((Label)_displayControl).Text.ToString();

// 增加key/value对到NewValues集合
e.NewValues.Add(key, value);
}
}
}
}

// 注册动态创建的客户端脚本
protected override void Render(HtmlTextWriter writer)
{
// 在RowDataBound中创建的自定义事件必须要在页中注册
// 通过重写Render方法来调用ClientScriptManager.RegisterForEventValidation。
// 通过GridViewRow.UniqueID返回行的唯一ID,按纽的唯一ID通过在行的唯一ID后附加“$ct100”而生成。
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
}
}
}

base.Render(writer);
}
}
cwmwss 2009-03-31
  • 打赏
  • 举报
回复
你百度一下gridveiw单元格编辑
林g 2009-03-31
  • 打赏
  • 举报
回复
没有用到过这样的功能
学习!
takako_mu 2009-03-31
  • 打赏
  • 举报
回复
我當初是用js+Xml+webservice實現的,不用任何控件。
webservice中獲取Dataset轉成Xml,js調用webservice,將xml再填進div,雙擊某單元格實現編輯、保存這些都是TD的onclick事件。我貼些關鍵代碼,如有疑問請跟帖。

/*--編輯事件-------------------------------------------------------------------------------------------------------*/
var input = document.createElement("input");//for edit cell
input.type="text" ;
var innerSelect = document.createElement("select");//模板列,Just like a Dropdownlist in a Gridview

var currentCell ;//得到当前的单元格的值,for strSQL
var id;//key
var cellRow;//抓(標題)字段在第幾列
var cellType;//抓(標題)字段

var temp;//for edit

function editCell(event)
{
if(event==null)
{
currentCell=window.event.srcElement;//IE:指向触发事件的元素,他是什么就有什么的属性
}
else
{
currentCell=event.target;//Firefox: firefox下的 event.target = IE 下的 event.srcElement
}
if(currentCell.tagName=="TD")//只為大寫!!
{
temp=currentCell.innerHTML;
var cellCol=currentCell.parentElement.rowIndex;//get col number,begin with 0
cellRow=currentCell.cellIndex;//get row number,bgein with 0
cellType=document.getElementById("__AJAX_GridView__div").rows[0].cells[cellRow].innerText;
id=document.getElementById("__AJAX_GridView__div").rows[cellCol].cells[0].innerText;//get key id
document.onkeydown = keyDown;//for esc.
if(cellType=="Project1")
{
GetPublicElement(2,"itemName");//模板列綁定
}
else
{
input.value=currentCell.innerHTML;//用单元格的值来填充文本框的值
input.onblur=last;//当文本框丢失焦点时调用last
input.ondblclick=last;
currentCell.innerHTML="";
currentCell.appendChild(input);//把文本框加到当前单元格上,不需要再從數據庫讀一遍數據
input.focus();
}
}
}

/*--編輯事件-------------------------------------------------------------------------------------------------------*/
w398687283 2009-03-31
  • 打赏
  • 举报
回复
最近做了个类似的,用gridview
里边放下拉框
DropDownList ddl= (DropDownList )sender;
GridViewRow row1 = (GridViewRow)ddl.Parent.Parent;
不知道是否符合LZ要求
major 2009-03-31
  • 打赏
  • 举报
回复
放一个gridview控件然后把其他的列设为只读,要编辑的列不设置改属性就可以了。
icheney 2009-03-31
  • 打赏
  • 举报
回复
回帖是一种美德!每天回帖即可获得 10 分可用分!
Nesta_13 2009-03-30
  • 打赏
  • 举报
回复
具体要实现的效果如下
fengjian_428 2009-03-30
  • 打赏
  • 举报
回复
用TABLE做可以 用GRID应该也可以,在dataitembind(大概是这个事件)里给每个cell添加onclick事件,然后在cell里加入一个textbox并保留原内容就行了。
我一般都是用utralwebgrid 一个第三方控件自动提供这样的功能
Nesta_13 2009-03-30
  • 打赏
  • 举报
回复
你的意思是还是通过table来实现?
没有.net的控件可以实现此功能吗?
加载更多回复(1)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧