datagrid中的按钮列中的编辑列的控制问题?

lianxtq 2003-07-21 09:54:41
datagrid中的按钮列中的编辑列的控制问题,当我采用这种方式进行修改时,系统默认的都是textbox框,而我们有时会需要比如dropdownlist,radiobutton等,怎么让他生成上面的这些表单,另外怎么修改生成的textbox的样式,默认的不好看??
...全文
37 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
renzhaxu 2003-07-21
  • 打赏
  • 举报
回复
选定某一列,变为模板列
然后右击,选择编辑模板
接着你可以直接拖控件到模板中
......
右击,结束编辑
其余的,你应该可以搞定了
lianxtq 2003-07-21
  • 打赏
  • 举报
回复
谢谢,哪天研究出来了通知一声,分照送:)
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
对于这个问题(在datagrid中用别的编辑控件),我曾经研究了好久,最后得出一个结论要实现这个是很难的,但datalist就不同了,它有专门设计编辑列的模块,随你自己定义,很方便的。
lianxtq 2003-07-21
  • 打赏
  • 举报
回复
你好,我要的是web方式的
你使用的是datalist,其中的radiobutton或dropdownlist控件都是已经设计好的,数据在显示时进行绑定,对吧,这是个好方法。
但我想问你通过datagrid怎么在编辑时实现dropdownlist控件,我想这两中方式还是有区别的?
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
如果是web环境下,就用DataList,sample如下(部分):
<asp:DataList ID="DataList1" ExtractTemplateRows="true" GridLines="Both" BorderColor="black" Font-Name="Verdana" Runat="server" Font-Size="8pt" CellPadding="3" OnCancelCommand="Cancel" OnItemCommand="Edit">
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<HeaderStyle BackColor="#aaaaee"></HeaderStyle>
<FooterStyle BackColor="#aaaaee"></FooterStyle>
<HeaderTemplate>
<asp:Table runat="server" ID="Table1">
<asp:TableRow>
<asp:TableCell>
编 号
</asp:TableCell>
<asp:TableCell>
姓 名
</asp:TableCell>
<asp:TableCell>
任 务
</asp:TableCell>
<asp:TableCell>
文 档
</asp:TableCell>
<asp:TableCell>
测试代码
</asp:TableCell>
<asp:TableCell>
代码行数
</asp:TableCell>
<asp:TableCell>
性 能
</asp:TableCell>
<asp:TableCell>
备 注
</asp:TableCell>
<asp:TableCell>
编 辑
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</HeaderTemplate>
<ItemTemplate>
<asp:table runat="server" ID="Table2">
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="Label1" Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label2" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Linkbutton ID="link1" Text='<%# DataBinder.Eval(Container.DataItem, "taskdetail") %>' Runat="server" commandname='taskd'></asp:Linkbutton>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label3" Text='<%# DataBinder.Eval(Container.DataItem, "hasdoc")%>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label4" Text='<%# DataBinder.Eval(Container.DataItem, "hastestcode")%>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label5" Text='<%# Convert.IsDBNull(DataBinder.Eval(Container.DataItem, "linecount"))?"":(Convert.ToInt64(DataBinder.Eval(Container.DataItem, "linecount")) != -1)?DataBinder.Eval(Container.DataItem, "linecount"):""%>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label6" Text='<%# DataBinder.Eval(Container.DataItem, "perform")%>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label7" Text='<%# DataBinder.Eval(Container.DataItem, "memo")%>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:LinkButton id="link2" commandname='Edit' text='<%# DataBinder.Eval(Container.DataItem, "edit") %>' runat=server></asp:LinkButton>
</asp:TableCell>
</asp:TableRow>
</asp:table>
</ItemTemplate>
<EditItemTemplate>
<asp:table runat="server" ID="Table3">
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="Label11" Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label12" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' Runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Linkbutton ID="link3" Text='<%# DataBinder.Eval(Container.DataItem, "taskdetail") %>' commandname=task Runat="server"></asp:Linkbutton>
</asp:TableCell>
<asp:TableCell>//单选按钮
<asp:radioButton id="Radio1" Text="Yes" checked='<%# (string)DataBinder.Eval(Container.DataItem, "hasdoc") == "Yes"%>' runat="server" BackColor="#E0E0E0" BorderStyle="None" groupname=doc/>
<asp:radioButton id="Radio2" Text="No" checked='<%# (string)DataBinder.Eval(Container.DataItem, "hasdoc") == "No"%>' runat="server" BackColor="#E0E0E0" BorderStyle="None" groupname=doc/>
</asp:TableCell>
<asp:TableCell>
<asp:radioButton id="Radio3" Text="Yes" checked='<%# (string)DataBinder.Eval(Container.DataItem, "hastestcode") == "Yes"%>' runat="server" BackColor="#E0E0E0" BorderStyle="None" groupname=code/>
<asp:radioButton id="Radio4" Text="No" checked='<%# (string)DataBinder.Eval(Container.DataItem, "hastestcode") == "No"%>' runat="server" BackColor="#E0E0E0" BorderStyle="None" groupname=code/>
</asp:TableCell>
<asp:TableCell>//文本框
<asp:textbox id=text3 BorderColor="Silver" backcolor="#E0E0E0" BorderStyle="Ridge" BorderWidth="1px" width=60 runat=server text='<%# Convert.IsDBNull(DataBinder.Eval(Container.DataItem, "linecount"))?"":(Convert.ToInt64(DataBinder.Eval(Container.DataItem, "linecount")) != -1)?DataBinder.Eval(Container.DataItem, "linecount"):""%>'/>
</asp:TableCell>
<asp:TableCell>//下拉框
<select id="Select1" Multiple="False" runat="server" value='<%# (string)DataBinder.Eval(Container.DataItem, "perform")==""?"-1":DataBinder.Eval(Container.DataItem, "perform")%>'>
<option value="-1" Selected="True"> </option>
<option value="优" Selected="True">优</option>
<option value="良">良</option>
<option value="中">中</option>
<option value="一般">一般</option>
<option value="差">差</option>
</select>
</asp:TableCell>
<asp:TableCell>
<asp:textbox id=text5 BorderColor="Silver" backcolor="#E0E0E0" BorderStyle="Ridge" BorderWidth="1px" runat=server text='<%# DataBinder.Eval(Container.DataItem, "memo") %>'/>
</asp:TableCell>
<asp:TableCell>
<asp:LinkButton Text="Save" Runat="server" CommandName='<%# DataBinder.Eval(Container.DataItem, "edit") %>'></asp:LinkButton>  
<asp:LinkButton Text="Cancel" Runat="server" CommandName="Cancel"></asp:LinkButton>
</asp:TableCell>
</asp:TableRow>
</asp:table>
</EditItemTemplate>
<FooterTemplate>
<asp:Table Runat="server" ID="Table4">
<asp:TableRow>
<asp:TableCell ColumnSpan="9" height=15>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</FooterTemplate>
</asp:DataList>
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
如果是forms下:
创建自己的 DataGridColumnStyle 对象集并将它们添加到 GridColumnStylesCollection 中来实现。
Sample如下(微软.net文档):

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridTimePickerColumn : DataGridColumnStyle
{
private DateTimePicker myDateTimePicker = new DateTimePicker();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;

public DataGridTimePickerColumn() : base()
{
myDateTimePicker.Visible = false;
}

protected override void Abort(int rowNum)
{
isEditing = false;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
Invalidate();
}

protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myDateTimePicker.Bounds = Rectangle.Empty;

myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);

if (!isEditing)
return true;

isEditing = false;

try
{
DateTime value = myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}

protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
DateTime value = (DateTime)
GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}

if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}

protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override int GetPreferredHeight(Graphics g,
object value)
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
DateTime date = (DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString("d"),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myDateTimePicker.Parent != null)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if (value != null)
{
value.Controls.Add(myDateTimePicker);
}
}

private void TimePickerValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myDateTimePicker);
}
}
namespace DataGridColumnStyleExample
{
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;


public class MyForm : Form
{
private DataTable namesDataTable;
private DataGrid grid = new DataGrid();
public MyForm() : base()
{
InitForm();

namesDataTable = new DataTable("NamesTable");
namesDataTable.Columns.Add(new DataColumn("Name"));
DataColumn dateColumn = new DataColumn
("Date", typeof(DateTime));
namesDataTable.Columns.Add(dateColumn);
DataSet namesDataSet = new DataSet();
namesDataSet.Tables.Add(namesDataTable);
grid.DataSource = namesDataSet;
grid.DataMember = "NamesTable";
AddGridStyle();
AddData();
}

private void AddGridStyle()
{
DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "NamesTable";

DataGridTextBoxColumn nameColumnStyle =
new DataGridTextBoxColumn();
nameColumnStyle.MappingName = "Name";
nameColumnStyle.HeaderText= "Name";
myGridStyle.GridColumnStyles.Add(nameColumnStyle);

DataGridTimePickerColumn timePickerColumnStyle =
new DataGridTimePickerColumn();
timePickerColumnStyle.MappingName = "Date";
timePickerColumnStyle.HeaderText = "Date";
timePickerColumnStyle.Width = 100;
myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);

grid.TableStyles.Add(myGridStyle);
}

private void AddData()
{

DataRow dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 1";
dRow["Date"] = new DateTime(2001, 12, 01);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 2";
dRow["Date"] = new DateTime(2001, 12, 04);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 3";
dRow["Date"] = new DateTime(2001, 12, 29);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 4";
dRow["Date"] = new DateTime(2001, 12, 13);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 5";
dRow["Date"] = new DateTime(2001, 12, 21);
namesDataTable.Rows.Add(dRow);

namesDataTable.AcceptChanges();
}

private void InitForm()
{
this.Size = new Size(500, 500);
grid.Size = new Size(350, 250);
grid.TabStop = true;
grid.TabIndex = 1;
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(grid);
}
[STAThread]
public static void Main()
{
MyForm myForm1= new MyForm();
myForm1.ShowDialog();
}
}
}
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
如果是forms下:
创建自己的 DataGridColumnStyle 对象集并将它们添加到 GridColumnStylesCollection 中来实现。
Sample如下(微软.net文档):

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridTimePickerColumn : DataGridColumnStyle
{
private DateTimePicker myDateTimePicker = new DateTimePicker();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;

public DataGridTimePickerColumn() : base()
{
myDateTimePicker.Visible = false;
}

protected override void Abort(int rowNum)
{
isEditing = false;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
Invalidate();
}

protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myDateTimePicker.Bounds = Rectangle.Empty;

myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);

if (!isEditing)
return true;

isEditing = false;

try
{
DateTime value = myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}

protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
DateTime value = (DateTime)
GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}

if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}

protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override int GetPreferredHeight(Graphics g,
object value)
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
DateTime date = (DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString("d"),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myDateTimePicker.Parent != null)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if (value != null)
{
value.Controls.Add(myDateTimePicker);
}
}

private void TimePickerValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myDateTimePicker);
}
}
namespace DataGridColumnStyleExample
{
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;


public class MyForm : Form
{
private DataTable namesDataTable;
private DataGrid grid = new DataGrid();
public MyForm() : base()
{
InitForm();

namesDataTable = new DataTable("NamesTable");
namesDataTable.Columns.Add(new DataColumn("Name"));
DataColumn dateColumn = new DataColumn
("Date", typeof(DateTime));
namesDataTable.Columns.Add(dateColumn);
DataSet namesDataSet = new DataSet();
namesDataSet.Tables.Add(namesDataTable);
grid.DataSource = namesDataSet;
grid.DataMember = "NamesTable";
AddGridStyle();
AddData();
}

private void AddGridStyle()
{
DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "NamesTable";

DataGridTextBoxColumn nameColumnStyle =
new DataGridTextBoxColumn();
nameColumnStyle.MappingName = "Name";
nameColumnStyle.HeaderText= "Name";
myGridStyle.GridColumnStyles.Add(nameColumnStyle);

DataGridTimePickerColumn timePickerColumnStyle =
new DataGridTimePickerColumn();
timePickerColumnStyle.MappingName = "Date";
timePickerColumnStyle.HeaderText = "Date";
timePickerColumnStyle.Width = 100;
myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);

grid.TableStyles.Add(myGridStyle);
}

private void AddData()
{

DataRow dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 1";
dRow["Date"] = new DateTime(2001, 12, 01);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 2";
dRow["Date"] = new DateTime(2001, 12, 04);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 3";
dRow["Date"] = new DateTime(2001, 12, 29);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 4";
dRow["Date"] = new DateTime(2001, 12, 13);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 5";
dRow["Date"] = new DateTime(2001, 12, 21);
namesDataTable.Rows.Add(dRow);

namesDataTable.AcceptChanges();
}

private void InitForm()
{
this.Size = new Size(500, 500);
grid.Size = new Size(350, 250);
grid.TabStop = true;
grid.TabIndex = 1;
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(grid);
}
[STAThread]
public static void Main()
{
MyForm myForm1= new MyForm();
myForm1.ShowDialog();
}
}
}
renzhaxu 2003-07-21
  • 打赏
  • 举报
回复
System.Web.UI.WebControls.TextBox tb1=
(Textbox)this.DataGrid1.Items[i].Cells[j].FindControl("textbox1");
string st=tb1.Text.Tostring();
renzhaxu 2003-07-21
  • 打赏
  • 举报
回复
System.Web.UI.WebControls.TextBox tb1=(TextBox)
(Textbox)this.DataGrid1.Items[i].Cells[j].FindControl("textbox1");
string st=tb1.Text.Tostring();
麻烦你自己测试了
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
quickstart/aspplus/samples/vstudio/webforms/data/cs/datagrid8.aspx
quickstart/aspplus/samples/vstudio/webforms/data/cs/datagrid8.aspx.cs
lianxtq 2003-07-21
  • 打赏
  • 举报
回复
renzhaxu(renzhaxu) :
你说的对,但还请咨询你,修改为模板列后,他自动会生成id为textbox1的text框,但取其值确不能用textbox1。text,该用什么方法取,(在产生DataGrid1_UpdateCommand事件时取)
luohzad 2003-07-21
  • 打赏
  • 举报
回复
使用模板列很容易实现。。
具体操作方法:lulei(卢雷) 回答的比较清楚。。
lulei 2003-07-21
  • 打赏
  • 举报
回复
使用为模板列
右击,选择编辑模板
直接拖控件到模板中

右击,结束编辑
1. 打开新的窗口并传送参数: 2.为按钮添加对话框 3.删除表格选定记录 4.删除表格记录警告 5.点击表格行链接另一页 6.表格超连接传递参数 7.表格点击改变颜色 8.关于日期格式 9.获取错误信息并到指定页面 10.清空Cookie 11.自定义异常处理 12.Panel 横向滚动,纵向自动扩展 13.回车转换成Tab 14.DataGrid超级连接 15.DataGrid行随鼠标变色 16.模板 17.数字格式化 18.日期格式化 19.如何设定全局变量 20.怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口? 21.读取DataGrid控件TextBox值 23.在DataGrid有3个模板包含Textbox分别为 DG_ShuLiang (数量) DG_DanJian(单价) DG_JinE(金额)分别在5.6.7,要求在录入数量及单价的时候自动算出金额即:数量*单价=金额还要求录入时限制为 数值型.我如何用客户端脚本实现这个功能? 24.datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了。 25.在Datagrid修改数据,当点击编辑键时,数据出现在文本框,怎么控制文本框的大小 ? 26.对话框 27. 将时间格式化:string aa=DateTime.Now.ToString("yyyy年MM月dd日"); 28.自定义分页代码: 29.DataGrid使用: 30.当文件在不同目录下,需要获取数据库连接字符串(如果连接字符串放在Web.config,然后在Global.asax初始化) 31. 变量.ToString() 32、变量.Substring(参数1,参数2); 33.在自己的网站上登陆其他网站:(如果你的页面是通过嵌套方式的话,因为一个页面只能有一个FORM,这时可以导向另外一个页面再提交登陆信息)
本文档不准备面面俱到地介绍DataGridView,而是着眼于深入地介绍一些技术点的高级特性。 本文档按逻辑分为5个章节,首先是结构和特性的概览,其次是内置的/单元格类型的介绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践。 大部分章节含有一个“Q & A”部分,来回答该章节相关的一些常见问题。注意,某些问题会由于知识点的关联性重复出现在多个章节。这些问题、答案及其附带的示例代码都包含在本文档的附录部分。 一、DataGridView技术点的高级特性。 11 1 何为DataGridView 11 1.1 DataGridView和DataGrid 之间的区别 11 1.2 DataGridView的亮点 12 2 DataGridView的结构 13 2.1 结构元素(Architecture Elements) 13 2.2 单元格和组(Cells and Bands) 13 2.3 DataGridView的单元格 (DataGridViewCell) 13 2.3.1 DataGridViewCell的工作机制 14 2.3.2 常见问题 15 2.4 DataGridView的(DataGridViewColumn) 16 2.5 DataGridView的编辑控件(Editing Controls) 16 2.6 DataGridViewRow 17 2.6.1 常见问题 17 3 /单元格类型揭密(column/cell types) 17 3.1 DataGridViewTextBoxColumn 18 3.2 DataGridViewCheckBoxColumn 19 3.3 DataGridViewImageColumn 19 3.4 DataGridViewButtonColumn 19 3.5 DataGridViewComboBoxColumn 20 3.5.1 DataError事件和ComboBox 20 3.5.2 常见问题 20 3.6 DataGridViewLinkColumn 21 4 操作数据(Working with Data) 21 4.1 数据输入和验证的相关事件 21 4.1.1 数据验证相关事件的顺序 21 4.1.2 验证数据 22 4.1.3 在新行的数据输入(Data Entry in the New Row) 22 4.1.3.1 显示新行 22 4.1.3.2 为生成的新行添加默认值 22 4.1.3.4 在新行输入数据 23 4.1.3.5 自定义新行的可视化效果 23 4.1.3.6 新行的排序 24 4.1.3.7 关于新行,还要注意: 24 4.1.3.8 Virtual Mode下的新行 24 4.2 关于Null值 24 4.2.1 NullValue属性 24 4.2.2 DataSourceNullValue属性 25 4.3 DataError事件 25 4.4 数据绑定模式(Databound modes) 26 4.4.1 非绑定模式(Unbound Mode) 26 4.4.2 绑定模式(Bound Mode) 26 4.4.2.1 有效的数据源 27 4.4.3 虚拟模式 27 4.4.4 混合模式 – 绑定与非绑定模式 27 4.4.5 常见问题 28 5 特性综览(Overview of features) 28 5.1 样式(Styling) 28 5.1.1 The DataGridViewCellStyle Class 29 5.1.2 Using DataGridViewCellStyle Objects 29 5.1.3 Style Inheritance 30 5.1.4 Setting Styles Dynamically 34 5.2 Custom painting 35 5.2.1 Paint Parts 35 5.3.1 在Windows窗体DataGridView控件调整大小选项 39 5.3.2 Resizing with the Mouse用鼠标调整大小 42 5.3.3 Automatic Sizing自动调整大小 43 5.3.4 Programmatic Resizing编程调整大小 45 5.3.5 Customizing Content-based Sizing Behavior自定义基于内容的调整大小行为 46 5.3.6 Content-based Sizing Options基于内容的调整大小选项 47 5.4 Selection modes选择模式 47 5.4.1 Programmatic Selection编程选择 49 5.5 滚动(Scrolling) 49 5.5.1 Scroll event Scroll事件 49 5.5.2 Scroll bars滚动条 50 5.5.3 Scrolling Properties滚动属性 50 5.6 Sorting排序 50 5.6.1 Programmatic Sorting编程排序 52 5.6.2 Custom Sorting自定义排序 53 5.6.3 Common questions and scenarios常见问题及案例 54 5.7 Border styles边框样式 55 5.7.1 Standard Border Styles标准边框样式 55 5.7.2 Advanced Border Styles高级边框风格 56 5.8 Enter-Edit modes输入,编辑模式 57 5.9 Clipboard copy modes剪贴板拷贝模式 58 5.10 Frozen columns/rows冻结的/行 60 5.11 Implementing Custom cells and editing controls/cells实现自定义和编辑控制单元格/单元格 60 5.11.1 IDataGridViewEditingControl 接口 61 5.11.2 IDataGridViewEditingCell 接口 61 5.12 Virtual mode虚拟模式 61 5.12.1 Bound Mode and Virtual Mode绑定模式和虚拟模式 62 5.12.2 Supplementing Bound Mode补充绑定模式 62 5.12.3 Common questions and scenarios常见问题及案例 62 5.12.4 Replacing Bound Mode更换绑定模式 63 5.12.5 Virtual-Mode Events虚拟模式事件 63 5.12.6 Best Practices in Virtual Mode在虚拟模式下的最佳实践 66 5.13 容量(Capacity) 66 6 最佳实践(Best Practices) 67 6.1 Using Cell Styles Efficiently使用高效单元格样式 67 6.2 Using Shortcut Menus Efficiently使用高效快捷菜单 68 6.3 Using Automatic Resizing Efficiently使用自动调整大小高效 69 6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently高效使用选定的单元格,行和的集合 69 6.5 Using Shared Rows 使用共享行 70 6.6 Preventing Rows from Becoming Unshared 防止行成为非共享 72 附录 A – FAQ 75 1. 如何使指定的单元格不可编辑? 75 2. 如何让一个单元格不可用(disable)? 75 3. 如何避免用户将焦点设置到指定的单元格? 77 4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)? 77 5. Why does the cell text show up with “square” characters where they should be new lines(TODO,未能实现该效果)? 78 6. 如何在单元格内同时显示图标和文本? 78 7. 如何隐藏一? 80 8. 如何避免用户对排序? 81 9. 如何针对多个排序? 81 9.1 将数据绑定到DataGridView时 81 9.2 Unbound DataGridView 取消绑定 82 9.2.1 Custom Sorting Using the SortCompare Event 使用排序结束时间实现用户自定义排序 82 9.2.2 Custom Sorting Using the IComparer Interface使用IComparer接口实现自定义排序 84 10. 如何为编辑控件添加事件处理函数? 86 11. 应在何时移除编辑控件的事件处理函数? 87 12. 如何处理ComboBox控件的SelectIndexChanged事件? 87 13. 如何通过拖放调整行的顺序? 87 14. 如何调整最后一的宽度使其占据网格的剩余客户区? 89 15. 如何让TextBox类型的单元格支持换行? 89 16. 如何使Image不显示任何图像(字段值为null时)? 90 17. 如何能够在ComboBox类型的单元格输入数据? 90 18. How do I have a combo box column display a sub set of data based upon the value of a different combo box column(TODO)? 91 19. 如何在用户编辑控件的时候(而不是在验证时)就显示错误图标? 92 20. 如何同时显示绑定数据和非绑定数据? 94 21. How do I show data that comes from two tables(TODO)?如何显示来自两个数据源的数据? 96 22. 如何显示主从表? 97 23. 如何在同一DataGridView显示主从表? 99 24. 如何避免用户对排序? 99 25. 如何在点击工具栏按钮的时候将数据提交到数据库? 99 26. 如何在用户删除记录时显示确认对话框? 99

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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