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

lianxtq 2003-07-21 09:54:41
datagrid中的按钮列中的编辑列的控制问题,当我采用这种方式进行修改时,系统默认的都是textbox框,而我们有时会需要比如dropdownlist,radiobutton等,怎么让他生成上面的这些表单,另外怎么修改生成的textbox的样式,默认的不好看??
...全文
27 13 打赏 收藏 举报
写回复
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
  • 打赏
  • 举报
回复
使用为模板列
右击,选择编辑模板
直接拖控件到模板中

右击,结束编辑
相关推荐
发帖
C#

10.9w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-21 09:54
社区公告

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