走了个datagridviwe,又来了个gridview。。
鼠标放上去变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddff';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
}
更新和删除
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow myrow = GridView1.Rows[e.RowIndex];
l.CID= ((TextBox)myrow.Cells[1].Controls[0]).Text;
l.CPhone = ((TextBox)myrow.Cells[2].Controls[0]).Text;
l.CarNum = ((TextBox)myrow.Cells[3].Controls[0]).Text;
l.NName = ((DropDownList)myrow.Cells[6].FindControl("mName")).SelectedValue.ToString();
TextBox tb = (TextBox)myrow.Cells[4].FindControl("rectime");
DateTime dt = DateTime.Parse(tb.Text.ToString());
l.LoadTime = dt.ToString("yyyy-MM-dd");
l.LoadsID = this.GridView1.DataKeys[myrow.RowIndex]["loadsID"].ToString();
if (l.LoadTime == l.getDate()) { }
else
{
l.updateLoadsInfo(dt);
}
l.updateLoads();
this.GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow myrow = GridView1.Rows[e.RowIndex];
l.LoadsID = this.GridView1.DataKeys[myrow.RowIndex]["loadsID"].ToString();
l.delete();
Bind();
}
加按钮
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ok")
{
int rowindex = Convert.ToInt32(e.CommandArgument);
// GridViewRow myrow = GridView1.Rows[rowindex];
l.LoadsID = GridView1.DataKeys[rowindex]["loadsID"].ToString();
l.earlyDelete(((TextBox)GridView1.Rows[rowindex].FindControl("chosetime")).Text);
Bind();
}
}
点击编辑固定列宽不变
<asp:BoundField DataField="cID" HeaderText="客户姓名" >
<ControlStyle Width="80px" />
<ItemStyle Width="80px" />
</asp:BoundField>
编辑模式下添加自己的控件
------添加时间控件-------
<asp:TemplateField HeaderText="放贷日期">
<ItemTemplate><%#eval_r("loadTime") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="rectime" runat="server" Text='<%# Bind("loadTime") %>' οnfοcus="WdatePicker({isShowClear:false,readOnly:false,dateFmt:'yyyy-MM-dd'})"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="rectime" Display="Dynamic" runat="server" ErrorMessage="日期不能为空" SetFocus />
</EditItemTemplate><ControlStyle Width="90px" />
<ItemStyle Width="90px" />
</asp:TemplateField>
------------添加按钮----------
<asp:TemplateField HeaderText="提前还款">
<ItemTemplate>
<asp:Button ID="trans" CommandName="ok" Text="提前还款" runat="server" OnClientClick="return confirm('是否确定提前?一旦确认,信息将不可找回。');" CommandArgument='<%# Container.DataItemIndex %>'
/>
</ItemTemplate></asp:TemplateField>
后台接
GridView1_RowCommand函数
点击传值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//下面三句代码可以实现你的效果,如果不合适,自己可以比照着修改一下。
string cellvalue = e.Row.Cells[0].Text;
string cellvalue2 = e.Row.Cells[2].Text;
// string js = string.Format("document.getElementByIdx_x("tname").value="854");");
string js2 = string.Format("document.getElementByIdx_x("TextBox1").value="{0}";document.getElementByIdx_x("TextBox2").value="{1}";", cellvalue, cellvalue2);
// string js = string.Format("alert("{0}");", cellvalue);
e.Row.Attributes.Add("onclick", js2);
//下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddff',this.style.fontWeight='Bold';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
}