<asp:GridView ID="GV_Humiture" runat="server"
Height="10px" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GV_Humiture_RowDataBound" OnRowCreated="GV_Humiture_RowCreated" BackColor="White" CssClass="font11" BorderColor="#ABC5E0" >
<FooterStyle BackColor="White" ForeColor="#001F42" BorderColor="#ABC5E0" BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="Date" HeaderText="日期" >
<HeaderStyle BorderColor="#001F42" Font-Bold="False" />
<ItemStyle ForeColor="#001F42" Width="6%" />
</asp:BoundField>
<asp:BoundField DataField="am_Dry" HeaderText="干(℃)" >
<ItemStyle ForeColor="#001F42" Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="am_Wet" HeaderText="湿(℃)" >
<ItemStyle ForeColor="#001F42" Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="am_Humidity" HeaderText="相对湿度(%)" >
<ItemStyle ForeColor="#001F42" Width="19%" />
</asp:BoundField>
<asp:BoundField DataField="pm_Dry" HeaderText="干(℃)" >
<ItemStyle ForeColor="#001F42" Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="pm_Wet" HeaderText="湿(℃)" >
<ItemStyle ForeColor="#001F42" Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="pm_Humidity" HeaderText="相对湿度(%)">
<ItemStyle ForeColor="#001F42" Width="19%" />
</asp:BoundField>
<asp:BoundField DataField="Note" HeaderText="备注" >
<ItemStyle ForeColor="#001F42" Width="7%" />
</asp:BoundField>
</Columns>
<AlternatingRowStyle BorderColor="#001F42" ForeColor="#001F42" />
<RowStyle Font-Names="Arial" ForeColor ="#ABC5E0" />
</asp:GridView>
怎么实现双击 GridView (GV_Humiture) 单元格后可以编辑单元格内容.
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.attributs.add("onDblClick", "...")
<ItemTemplate >
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("InfManageID", "~/Web/InfManage/InfManageSee.aspx?InfManageID={0}") %>'
Text='<%# Eval("Title") %>' Target="_blank"></asp:HyperLink>
<asp:Image ID="ImageNew" runat="server" visible="false" Width="25" Height="10" />
<asp:HiddenField ID="HF_ContentOrLink" runat="server" value='<%# Eval("ContentOrLink") %>'/>
</ItemTemplate>
<ItemStyle Width="80%" />
</asp:TemplateField>
<%# Eval("Title") %>' 使用这个绑定 难道 非得 DataSourceID="SqlDataSource1" 不能直接从后台给定一个DT 绑定吗?
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick"
Visible="false" />
</Columns>
rowdatabound中:
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[1].Controls[0];
string _jsDouble = ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
e.Row.Attributes["ondblclick"] = _jsDouble;
}然后在RowCommand中判断CommandName
int _rowIndex = int.Parse(e.CommandArgument.ToString());
// 解析事件参数(在RowDataBound中增加的),从而获得被选中的列的索引
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
// 设置GridView被选中的行的索引(每次回发后判断GridView1.SelectedIndex > -1则更新)
_gridView.SelectedIndex = _rowIndex;
_gridView.DataBind();// 获得被选中单元格的显示控件并设置其不可见
Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[..];
_displayControl.Visible = false;
// 获得被选中单元格的编辑控件并设置其可见
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[..];
_editControl.Visible = true;
GridView72般绝技
里面有N多例子!!!
LZ看看有没有帮助~
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow Row in GridView1.Rows)
{
if (Row.RowType == DataControlRowType.DataRow)
{
//双击进入编辑模式
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + Row.RowIndex.ToString(), true);
Row.Attributes["style"] = "cursor:pointer";
Row.Attributes["title"] = "双击进入编辑";
if (Row.RowState == DataControlRowState.Edit)
{
Row.Attributes.Remove("ondblclick");
Row.Attributes.Remove("style");
Row.Attributes["title"] = "编辑行";
for (Int32 i = 1; i < GridView1.Columns.Count; i++)
{
((TextBox)Row.Cells[i].Controls[1]).Attributes.Add("onmouseover", "this.select()"); }
//双击更新
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Update$" + Row.RowIndex.ToString(), true); }
//
for (int i = 1; i < Row.Cells.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(Row.UniqueID + "$ctl00", i.ToString());
}
}
}
base.Render(writer);
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow Row in GridView1.Rows)
{
if (Row.RowType == DataControlRowType.DataRow)
{
//双击进入编辑模式
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + Row.RowIndex.ToString(), true);
Row.Attributes["style"] = "cursor:pointer";
Row.Attributes["title"] = "双击进入编辑";
if (Row.RowState == DataControlRowState.Edit)
{
Row.Attributes.Remove("ondblclick");
Row.Attributes.Remove("style");
Row.Attributes["title"] = "编辑行";
for (Int32 i = 1; i < GridView1.Columns.Count; i++)
{
((TextBox)Row.Cells[i].Controls[1]).Attributes.Add("onmouseover", "this.select()"); }
//双击更新
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Update$" + Row.RowIndex.ToString(), true); }
//
for (int i = 1; i < Row.Cells.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(Row.UniqueID + "$ctl00", i.ToString());
}
}
}
base.Render(writer);
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow Row in GridView1.Rows)
{
if (Row.RowType == DataControlRowType.DataRow)
{
//双击进入编辑模式
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + Row.RowIndex.ToString(), true);
Row.Attributes["style"] = "cursor:pointer";
Row.Attributes["title"] = "双击进入编辑";
if (Row.RowState == DataControlRowState.Edit)
{
Row.Attributes.Remove("ondblclick");
Row.Attributes.Remove("style");
Row.Attributes["title"] = "编辑行";
for (Int32 i = 1; i < GridView1.Columns.Count; i++)
{
((TextBox)Row.Cells[i].Controls[1]).Attributes.Add("onmouseover", "this.select()"); }
//双击更新
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Update$" + Row.RowIndex.ToString(), true); }
//
for (int i = 1; i < Row.Cells.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(Row.UniqueID + "$ctl00", i.ToString());
}
}
}
base.Render(writer);
}
{
foreach (GridViewRow Row in GridView1.Rows)
{
if (Row.RowType == DataControlRowType.DataRow)
{
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + Row.RowIndex.ToString(), true);
Row.Attributes["style"] = "cursor:pointer";
Row.Attributes["title"] = "双击进入编辑";
if (Row.RowState == DataControlRowState.Edit)
{
Row.Attributes.Remove("ondblclick");
Row.Attributes.Remove("style");
Row.Attributes["title"] = "编辑行";
for (Int32 i = 1; i < GridView1.Columns.Count; i++)
{
((TextBox)Row.Cells[i].Controls[1]).Attributes.Add("onmouseover", "this.select()");
}
Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Update$" + Row.RowIndex.ToString(), true); }
for (int i = 1; i < Row.Cells.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(Row.UniqueID + "$ctl00", i.ToString());
}
}
}
base.Render(writer);
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{//更新数据
}
参考