Row内找Label控件
//内部控件事件
protected void LinkButton11_Click1(object sender, EventArgs e)
{
//删除对应的事务
int itemid = 0;
GridViewRow gr = (GridViewRow)(((LinkButton)sender).Parent.Parent);
Label l = gr.FindControl("lbRoleName") as Label;
if (l == null)
{
}
else
{
itemid = int.Parse(l.Text.ToString());
}
/
<asp:GridView ID="grdvIdeal" runat="server" BackColor="White" BorderColor="#000000"
BorderStyle="solid" BorderWidth="1px" CellPadding="1" Width="436px" PageSize="6"
onselectedindexchanged="grdvIdeal_SelectedIndexChanged1"
AutoGenerateSelectButton="True" CssClass="gridviewtxt"
onrowdatabound="grdvIdeal_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
//选择单条的意见进行显示
protected void grdvIdeal_SelectedIndexChanged1(object sender, EventArgs e)
{
//选择单条的意见进行显示
try
{
txtShow.Text = grdvIdeal.Rows[grdvIdeal.SelectedIndex].Cells[3].Text;
}
catch
{
txtShow.Text = "选择出错!";
}
}
//控制数据绑定效果
protected void grdvIdeal_RowDataBound(object sender, GridViewRowEventArgs e)//约束字符长度
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strName1 = e.Row.Cells[2].Text;
if (strName1.Length > 4)
{
e.Row.Cells[2].Text = strName1.Substring(0, 4) + "...";
}
e.Row.Cells[2].ToolTip = strName1;
string strName2 = e.Row.Cells[3].Text;
if (strName2.Length > 4)
{
e.Row.Cells[3].Text = strName2.Substring(0, 4) + "...";
}
e.Row.Cells[3].ToolTip = strName2;
}
//
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MemName" onrowdatabound="GridView1_RowDataBound" ForeColor="#333333" Width="508px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:Label runat="server" ID="lbMemName" ForeColor='black' Text='<%# Eval("MemName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容">
<ItemTemplate>
<asp:Label runat="server" ID="lbRAPLMatter" ForeColor='black' Text='<%# Eval("RAPLMatter")%>' ToolTip ='<%# Eval("RAPLMatter")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="整改信息">
<ItemTemplate>
<asp:Label runat="server" ID="lbMendMeasure" ForeColor='black' Text='<%# Eval("MendMeasure")%>' ToolTip ='<%# Eval("MendMeasure")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="管理员">
<ItemTemplate>
<asp:Label runat="server" ID="lbManager" ForeColor='black' Text='<%# Eval("Manager")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="提交时间">
<ItemTemplate>
<asp:Label runat="server" ID="lbRAPLTime" ForeColor='black' Text='<%# Eval("RAPLTime")%>'/>
</ItemTemplate>
</asp:TemplateField></Columns>
</asp:GridView>
//控制数据绑定效果
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strName = "";
if (e.Row.RowType == DataControlRowType.DataRow)
{
strName = e.Row.Cells[1].Text.ToString();
if (strName.Length > 5)
e.Row.Cells[1].Text = strName.Substring(0, 5) + "...";
e.Row.Cells[1].ToolTip = strName;
}
}
// //
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4"
ForeColor="#333333" GridLines="None"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound" PageSize="6" Width="508px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
//分页绑定
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
//控制数据绑定效果
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
string strName = e.Row.Cells[1].Text;
if(strName.Length>5)
{
e.Row.Cells[1].Text = strName.Substring(0, 5) + "...";
}
e.Row.Cells[1].ToolTip = strName;
}
}
// 类似DataList控件的编辑项
<asp:GridView ID="gvPlant" runat="server" AutoGenerateColumns="False" Width="550px"
GridLines="None" Font-Size="Small" BorderStyle="Solid" BorderColor="#BCCDFE"
BorderWidth="1px" DataKeyNames="ItemId" OnRowDataBound="gvPlant_RowDataBound"
onselectedindexchanged="gvPlant_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName" ForeColor='black' Text='<%# Eval("ItemId")%>' ToolTip ='<%# Eval("ItemMatter")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="事务名称">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName1" ForeColor='black' Text='<%# GetShot(Eval("ItemName"))%>' ToolTip ='<%# Eval("ItemName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="负责人">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName2" ForeColor='black' Text='<%# Eval("MemName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="申请日期">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName3" ForeColor='black' Text='<%# Eval("ReferTime")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="期限(天)">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName4" ForeColor='black' Text='<%# Eval("DisposeTime")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="审批状态">
<ItemTemplate>
<asp:Label runat="server" ID="lbRoleName6" ForeColor='black' Text='<%# Station(Eval("Sta"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="审批">
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%#"ExamineAndApproveAddEaa1WF.aspx"%>'><asp:Label ID="lblExma" runat="server" Text='<%# Examine()%>'></asp:Label></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="查看详情">
<ItemTemplate>
<asp:HyperLink ID="addintem" runat="server" NavigateUrl='<%#"IndexOnePlantItemProcessWF.aspx?ItemId=" +Eval("ItemId")+"&Sta="+Eval("Sta")+"&PrincipalId="+Eval("PrincipalId")%>'>查看详情</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="修改">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"AmendstudyOutWF.aspx?ItemId=" +Eval("ItemId")%>'><asp:Label ID="lblAmend" runat="server" Text='<%# Amend(Eval("Sta"),Eval("PrincipalId"))%>'></asp:Label></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton11" runat="server"
CommandName='<%# Eval("ItemId")%>' οnclick="LinkButton11_Click1" OnClientClick="if(window.confirm('这将删除和此事务相关的所有信息,你确定要删除吗?')==true){return true;}else{return false;}"><asp:Label ID="lblDel" runat="server" Text='<%# Amend(Eval("Sta"),Eval("PrincipalId"))%>'></asp:Label></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
//控制数据绑定效果
protected void gvPlant_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strName = "";
if (e.Row.RowType == DataControlRowType.DataRow)
{
strName = e.Row.Cells[0].Text.ToString();
if (strName.Length > 5)
e.Row.Cells[0].Text = strName.Substring(0, 5) + "...";
e.Row.Cells[0].ToolTip = strName;
}
}
//内部控件事件
protected void LinkButton11_Click1(object sender, EventArgs e)
{
//删除对应的事务
int itemid = 0;
GridViewRow gr = (GridViewRow)(((LinkButton)sender).Parent.Parent);
Label l = gr.FindControl("lbRoleName") as Label;
if (l == null)
{
}
else
{
itemid = int.Parse(l.Text.ToString());
}
planAndArrangeManageDal.DelStudyOut(itemid);//调用方法
gvPlant.DataSource = planAndArrangeManageDal.ShowItemInfo("link");
gvPlant.DataBind();
Page.RegisterStartupScript("", "<script>alert('删除成功!')</script>");
}
/
<asp:GridView ID="GridView1" runat="server" Width="795px"
onrowdatabound="GridView1_RowDataBound" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="3"
onselectedindexchanging="GridView1_SelectedIndexChanging" Font-Size="12px">
<Columns>
<asp:CommandField SelectText="查看" ShowSelectButton="True" />
</Columns>
</asp:GridView>
//控制数据绑定效果
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for(int i=3;i<7;i++)
if(e.Row.RowType==DataControlRowType.DataRow)
{
string strName = e.Row.Cells[i].Text.ToString();
if (strName.Length > 6)
e.Row.Cells[i].Text = strName.Substring(0, 6) + "...";
e.Row.Cells[i].ToolTip = strName;
}
}
//编辑性分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BLL.LogMamage logMamage = new LogMamage();
int memId = Convert.ToInt32(drmemName.SelectedValue.ToString());
string str1 = txtStart.Text.ToString();
string str2 = txtEnd.Text.ToString();
GridView1.DataSource = logMamage.ClassifySee(memId, str1, str2);
GridView1.DataBind();
}
//指针选择改变给其他控件赋值
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
try
{
workText.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].ToolTip.ToString();
workResult.Text = GridView1.Rows[e.NewSelectedIndex].Cells[4].ToolTip.ToString();
workQuestion.Text = GridView1.Rows[e.NewSelectedIndex].Cells[5].ToolTip.ToString();
docMemo.Text = GridView1.Rows[e.NewSelectedIndex].Cells[6].ToolTip.ToString();
lbname.Text = drmemName.SelectedItem.Text.ToString();
lbTime.Text = GridView1.Rows[e.NewSelectedIndex].Cells[7].Text.ToString();
}
catch
{
Page.RegisterStartupScript("", "<script>alert('系统维护中!'')</script>");
}
}
//
//指针滑动颜色控制
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
for (i = 0; i <= GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//单击行的任意列会自动选中此行
e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
}
}
}
//表格点击改变颜色
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","this.style.backgroundColor='#99cc00';
this.style.color='buttontext';this.style.cursor='default';");
}
写在DataGrid的_ItemDataBound里
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#99cc00';
this.style.color='buttontext';this.style.cursor='default';");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='';this.style.color='';");
}
//DataGrid超级连接列
DataNavigateUrlField="字段名" DataNavigateUrlFormatString="http://xx/inc/delete.aspx?ID={0}"
//DataGrid行随鼠标变色
private void DGzf_ItemDataBound
(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType!=ListItemType.Header)
{
e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=
/""+e.Item.Style["BACKGROUND-COLOR"]+"/"");
e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=/""+ "#EFF3F7"+"/"");
}
}
读取DataGrid控件TextBox值
foreach(DataGrid dgi in yourDataGrid.Items)
{
TextBox tb = (TextBox)dgi.FindControl("yourTextBoxId");
tb.Text....
}
datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了
page_load
page.smartNavigation=true
24.在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 ?
private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)
{
for(int i=0;i<e.Item.Cells.Count-1;i++)
if(e.Item.ItemType==ListItemType.EditType)
{
e.Item.Cells[i].Attributes.Add("Width", "80px")
}
}