gpt4 book ai didi

c# - 在 GridView 中操作当前编辑的行

转载 作者:太空狗 更新时间:2023-10-30 01:22:17 25 4
gpt4 key购买 nike

我正在努力使 gridview 的编辑功能更智能一些。

作为第一步,我希望当我为一行按下“编辑”时所有包含 DateTime 数据的列都会有一个日期选择器而不是文本框看来我的尝试只能编辑后面的行,不能编辑当前显示的编辑项。

我的目标是将我正在编辑的行中的日期列更改为日期选择器。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
var row = ((GridView)sender).Rows[e.NewEditIndex].Cells;

for (int i = 0; i < row.Count; i++)
{
DateTime dtDate;
var res = DateTime.TryParse(row[i].Text, out dtDate);
if (res)
{
DatePickerControl.DatePicker text = new DatePickerControl.DatePicker();
text.CalendarDate = dtDate;
row[i].Controls.Clear();
row[i].Controls.Add(text);
}
}
}

但它似乎编辑了后面的行,我怎样才能让它编辑我正在编辑的内容?

最佳答案

即使方法不同并且对于您的场景来说可能有点过于简单,我还是建议您使用 <asp:TemplateField />为了自定义编辑项模板(见下文)。当然,您可能还想替换 <asp:Calendar />我与您的自定义一起使用的控件 DatePickerControl.DatePicker控制。

<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Calendar runat="server" SelectedDate='<%# Eval("Date") %>'></asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Date") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

[更新]

您还可以动态创建列。请检查以下代码:

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.AutoGenerateColumns = false;
GridView1.AutoGenerateEditButton = true;

DataTable dataSource = new DataTable();
dataSource.Columns.Add("Id", typeof(int));
dataSource.Columns.Add("Date1", typeof(DateTime));
dataSource.Columns.Add("Date2", typeof(DateTime));
dataSource.Rows.Add(1, DateTime.Now, DateTime.Now.AddMonths(1));
dataSource.Rows.Add(2, DateTime.Now.AddMonths(2), DateTime.Now.AddMonths(3));

GridView1.Columns.Clear();
foreach (DataColumn column in dataSource.Columns)
{
if (column.DataType == typeof(DateTime))
{
var templateColumn = new TemplateField();
templateColumn.EditItemTemplate = new AddTemplateToGridView(ListItemType.EditItem, column.ColumnName);
templateColumn.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, column.ColumnName);
templateColumn.HeaderText = column.ColumnName;
GridView1.Columns.Add(templateColumn);
}
else
{
var dataBoundColumn = new BoundField();
dataBoundColumn.DataField = column.ColumnName;
dataBoundColumn.HeaderText = column.ColumnName;
GridView1.Columns.Add(dataBoundColumn);
}
}

GridView1.DataSource = dataSource;
GridView1.DataBind();

}

public class AddTemplateToGridView : ITemplate
{

ListItemType _type;
string _colName;

public AddTemplateToGridView(ListItemType type, string colname)
{
_type = type;
_colName = colname;
}

public void InstantiateIn(Control container)
{
switch (_type)
{
case ListItemType.Item:

Label l = new Label();
l.DataBinding += l_DataBinding;
container.Controls.Add(l);

break;
case ListItemType.EditItem:

Calendar calendar = new Calendar();
calendar.DataBinding += l_DataBinding;
container.Controls.Add(calendar);

break;
}
}

void l_DataBinding(object sender, EventArgs e)
{
GridViewRow container;
object dataValue;
switch (sender.GetType().ToString())
{
case "System.Web.UI.WebControls.Label":
Label label = (Label)sender;
container = (GridViewRow)label.NamingContainer;
dataValue = DataBinder.Eval(container.DataItem, _colName);
if (dataValue != DBNull.Value)
{
label.Text = dataValue.ToString();
}
break;
//use the DatePickerControl.DatePicker type instead of calendar
case "System.Web.UI.WebControls.Calendar":
Calendar calendar = (Calendar)sender;
container = (GridViewRow)calendar.NamingContainer;
dataValue = DataBinder.Eval(container.DataItem, _colName);
if (dataValue != DBNull.Value)
{
calendar.SelectedDate = (DateTime)dataValue;
calendar.VisibleDate = (DateTime)dataValue;
}
break;
}
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}

}

关于c# - 在 GridView 中操作当前编辑的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com