关于DataGrid的DataGridTableStyle的问题??

pbfan 2003-08-18 05:47:10
在微软的一篇文章中看到,可以利用DatagridTableStyle在运行过程中来调整各个列的长度和高度(类似于winform中的DataGrid),大家有实现过这样的功能吗??谢谢。。。
...全文
45 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
goody9807 2003-08-18
  • 打赏
  • 举报
回复

7.DataGrid需要动态帮定不同的列,如何控制他的列宽?

----if(e.Item.ItemType==ListItemType.Header)
for(int i=0,j=e.Item.Cells.Count;i<j;i++){
e.Item.Cells[i].Width=System.Web.UI.WebControls.Unit.Pixel(20);
}

--- DataGrid1.Columns(9).HeaderStyle.Width = Unit.Pixel(100)
goody9807 2003-08-18
  • 打赏
  • 举报
回复
http://www.codeproject.com/aspnet/masterdetailgrid/masterdetailgrid_src.zip
http://xml.sz.luohuedu.net/xml/Content.asp
http://aspalliance.com/Colt/Articles/Article3.aspx
http://xml.sz.luohuedu.net/xml/ShowList.asp?id=1

2、设置绑定列或者自动生成列的编辑框宽度
请在你的ItemDataBound事件中加入一下代码:
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i = 0; i < e.Item.Cells.Count; i++)
{
TextBox txt = (TextBox)e.Item.Cells[i].Controls[0];
txt.Width = Unit.Pixel(50);
}
}
pbfan 2003-08-18
  • 打赏
  • 举报
回复
那就是说没有办法实现在运行是改变列的宽度咯???
请高手指点。。。。
chnking 2003-08-18
  • 打赏
  • 举报
回复
aspx.net中的datagrid没有DatagridTableStyle属性,不过它有:
ItemStyle
EditItemStyle
AlternatingItemStyle
FooterStyle
HeaderStyle
这些属性来控制行的风格

还有:
DataGridColumn.ItemStyle 来控制列的风格
xwy2258588 2003-08-18
  • 打赏
  • 举报
回复
up
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace AddressList { /// /// Form1 的摘要说明。 /// public class AddressList : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox AddInfo; private System.Windows.Forms.Label lblName; private System.Windows.Forms.Label lblHomePhone; private System.Windows.Forms.Label lblOfficePhone; private System.Windows.Forms.Label lblHandSet; private System.Windows.Forms.Label lblTouchAddress; private System.Windows.Forms.Label lblPostalcode; private System.Windows.Forms.Label lblEmail; private System.Windows.Forms.TextBox txtName; private System.Windows.Forms.TextBox txtHomePhone; private System.Windows.Forms.TextBox txtOfficePhone; private System.Windows.Forms.TextBox txtHandSet; private System.Windows.Forms.TextBox txtTouchAddress; private System.Windows.Forms.TextBox txtPostalCode; private System.Windows.Forms.TextBox txtEmail; private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.GroupBox ExtendOperation; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button btnModify; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.Button btnSelect; private System.Windows.Forms.Label lblNote; private System.Windows.Forms.DataGrid dataGrid1; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public AddressList() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new AddressList()); } private void btnAdd_Click(object sender, System.EventArgs e) { AddInfo add=new AddInfo(); int ret=add.Add(this.txtName.Text,this.txtHomePhone.Text,this.txtOfficePhone.Text,this.txtHandSet.Text,this.txtTouchAddress.Text,this.txtPostalCode.Text,this.txtEmail.Text); if(ret>0) { MessageBox.Show("恭喜你,添加成功!"); bind(); } else { MessageBox.Show("添加失败!原因可能是你输入了重复的名称!"); } this.txtName.Text=""; this.txtHomePhone.Text=""; this.txtOfficePhone.Text=""; this.txtHandSet.Text=""; this.txtTouchAddress.Text=""; this.txtPostalCode.Text=""; this.txtEmail.Text=""; } private void AddressList_Load(object sender, System.EventArgs e) { bind(); DataGridTableStyle ts1= new DataGridTableStyle(); ts1.MappingName="BasicInfo"; DataGridColumnStyle cs = new DataGridTextBoxColumn(); cs.Alignment=HorizontalAlignment.Center; cs.MappingName="SName"; cs.HeaderText="姓名"; cs.Width=80; ts1.GridColumnStyles.Add(cs); DataGridColumnStyle cs1 =new DataGridTextBoxColumn(); cs1.Alignment=HorizontalAlignment.Center; ; cs1.MappingName="HomePhone"; cs1.HeaderText="住宅电话"; cs1.Width=80; ts1.GridColumnStyles.Add(cs1); DataGridColumnStyle cs2 =new DataGridTextBoxColumn(); cs2.Alignment=HorizontalAlignment.Center; ; cs2.MappingName="OfficePhone"; cs2.HeaderText="办公电话"; cs2.Width=80; ts1.GridColumnStyles.Add(cs2); DataGridColumnStyle cs3 = new DataGridTextBoxColumn(); cs3.Alignment=HorizontalAlignment.Center; cs3.MappingName="HandSet"; cs3.HeaderText="手机号码"; cs3.Width=80; ts1.GridColumnStyles.Add(cs3); DataGridColumnStyle cs4 = new DataGridTextBoxColumn(); cs4.Alignment=HorizontalAlignment.Center; cs4.MappingName="TouchAddress"; cs4.HeaderText="通信地址"; cs4.Width=200; ts1.GridColumnStyles.Add(cs4); DataGridColumnStyle cs5 = new DataGridTextBoxColumn(); cs5.Alignment=HorizontalAlignment.Center; cs5.MappingName="PostalCode"; cs5.HeaderText="邮政编码"; cs5.Width=60; ts1.GridColumnStyles.Add(cs5); DataGridColumnStyle cs6 = new DataGridTextBoxColumn(); cs6.Alignment=HorizontalAlignment.Center; cs6.MappingName="Email"; cs6.HeaderText="电子邮件"; cs6.Width=150; ts1.GridColumnStyles.Add(cs6); this.dataGrid1.TableStyles.Add(ts1); } public void bind() { SearchInfo result = new SearchInfo(); DataSet view = result.GetAddInfo(); this.dataGrid1.DataSource=view; this.dataGrid1.DataMember="BasicInfo"; } private void btnModify_Click(object sender, System.EventArgs e) { UpdateBasicInfo update=new UpdateBasicInfo(); int ret=update.Update(this.txtName.Text,this.txtHomePhone.Text,this.txtOfficePhone.Text,this.txtHandSet.Text,this.txtTouchAddress.Text,this.txtPostalCode.Text,this.txtEmail.Text); if(ret>0) { bind(); MessageBox.Show("修改成功,请核对!"); } else { MessageBox.Show("修改失败!"); } } private void dataGrid1_Click(object sender, System.EventArgs e) { this.btnModify.Enabled=true; this.btnDelete.Enabled=true; int row=this.dataGrid1.CurrentCell.RowNumber; this.txtName.Text=this.dataGrid1[row,0].ToString(); this.txtHomePhone.Text=this.dataGrid1[row,1].ToString(); this.txtOfficePhone.Text=this.dataGrid1[row,2].ToString(); this.txtHandSet.Text=this.dataGrid1[row,3].ToString(); this.txtTouchAddress.Text=this.dataGrid1[row,4].ToString(); this.txtPostalCode.Text=this.dataGrid1[row,5].ToString(); this.txtEmail.Text=this.dataGrid1[row,6].ToString(); } private void btnDelete_Click(object sender, System.EventArgs e) { DeleteBasicInfo del= new DeleteBasicInfo(); int ret = del.deleteInfo(this.txtName.Text); if (ret==0) { MessageBox.Show("删除失败!"); } else { bind(); MessageBox.Show("删除成功!"); } } private void btnSelect_Click(object sender, System.EventArgs e) { SearchInfo search=new SearchInfo(); DataSet result=search.searchInfo(this.txtName.Text); this.dataGrid1.DataSource=result; this.dataGrid1.DataMember="BasicInfo"; } } }

62,025

社区成员

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

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

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

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