Anonymous
Bearbeiten Sie automatisch generierte Spalten in der Telerik-Benutzeroberfläche
Post
by Anonymous » 08 Jan 2026, 08:10
Ich versuche, mehrere Zeilen in automatisch generierten Spalten von radgrid inline zu bearbeiten. Unten sind die Methoden zum Erstellen von Spalten und zum Erstellen von Elementen aufgeführt, die ich dafür geschrieben habe.
Hier habe ich manuelle Vorlagen erstellt, wie Sie den Code unten einchecken können.
Die Bearbeitung, die ich zu erhalten versuche, ist die folgende, aber ich erhalte das automatische Bearbeitungsraster, das von Griddataitem stammt.
Und der Code dafür ist
Aus diesem Grund bekomme ich die Änderung, aber
ich möchte das nicht.
Ich möchte eine Inline-Bearbeitung; Dafür habe ich die Vorlage durch die Vorlage ersetzt, die ich hier geschrieben habe.
Außerdem ist beim Debuggen der Index in „ReplaceWithTemplateColumn -1, da die Spalte keinen Index sammelt.
und ItemCreated:
Code: Select all
if (!(e.Column is GridBoundColumn boundCol))
return;
string field = boundCol.DataField;
// autocomplete
if (field == "Org" || field == "Dest" || field == "VIA_Station")
{
ReplaceWithTemplateColumn(
e, boundCol,
new DisplayTextTemplate(field),
new AutoCompleteEditTemplate(field)
);
}
else if (field == "Level")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.SRA_Level)
);
}
else if (field == "Carrier")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.Carrier)
);
}
else if (field == "Product")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.Product)
);
}
else if (field == "Rate_UOM")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.RateUOM)
);
}
else if (field == "Currency")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.Currency)
);
}
else if (field == "Commodity")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DropDownEditTemplate(field, CacheTable.Commodity)
);
}
else if (field == "Effective_Dt" || field == "Expiry_Dt")
{
ReplaceWithTemplateColumn(
e,
boundCol,
new DisplayTextTemplate(field),
new DatePickerEditTemplate(field)
);
}
/*
private void ReplaceWithTemplateColumn(
GridColumnCreatedEventArgs e,
GridBoundColumn oldCol,
ITemplate itemTemplate,
ITemplate editTemplate)
{
GridTemplateColumn tpl = new GridTemplateColumn
{
UniqueName = oldCol.UniqueName,
DataField = oldCol.DataField,
HeaderText = oldCol.HeaderText,
SortExpression = oldCol.SortExpression,
ItemTemplate = itemTemplate
};
if (editTemplate != null)
tpl.EditItemTemplate = editTemplate;
tpl.HeaderStyle.Width = oldCol.HeaderStyle.Width;
tpl.ItemStyle.Width = oldCol.ItemStyle.Width;
e.OwnerTableView.Columns.Remove(oldCol);
e.OwnerTableView.Columns.Add(tpl);
}
*/
private void ReplaceWithTemplateColumn(
GridColumnCreatedEventArgs e,
GridBoundColumn oldColumn,
ITemplate itemTemplate,
ITemplate editTemplate)
{
GridTemplateColumn newCol = new GridTemplateColumn();
// Preserve identity
newCol.UniqueName = oldColumn.UniqueName;
newCol.HeaderText = oldColumn.HeaderText;
newCol.SortExpression = oldColumn.SortExpression;
newCol.DataField = oldColumn.DataField;
// Preserve layout
newCol.HeaderStyle.Width = oldColumn.HeaderStyle.Width;
newCol.ItemStyle.Width = oldColumn.ItemStyle.Width;
newCol.ItemStyle.HorizontalAlign = oldColumn.ItemStyle.HorizontalAlign;
// Templates
newCol.ItemTemplate = itemTemplate;
if (editTemplate != null)
newCol.EditItemTemplate = editTemplate;
// replace at same index
// int index = Array.IndexOf(e.OwnerTableView.RenderColumns, e.Column);
int index = e.OwnerTableView.Columns.IndexOf(oldColumn);
if (index < 0)
{
return; // column is not in collection anymore
}
e.OwnerTableView.Columns.Remove(oldColumn);
e.OwnerTableView.Columns.Insert(index, newCol);
}
public class DisplayTextTemplate : ITemplate
{
private readonly string _uniqueName;
public DisplayTextTemplate(string uniqueName)
{
_uniqueName = uniqueName;
}
public void InstantiateIn(Control container)
{
Literal lbl = new Literal();
lbl.DataBinding += (s, e) =>
{
Literal l = (Literal)s;
GridDataItem item =
(GridDataItem)l.NamingContainer;
string text = item[_uniqueName].Text;
l.Text = (!string.IsNullOrWhiteSpace(text) && text != " ")
? text
: string.Empty;
};
container.Controls.Add(lbl);
}
}
public class DropDownEditTemplate : ITemplate
{
private readonly string _columnKey;
private readonly CacheTable _cacheTable;
public DropDownEditTemplate(string columnKey, CacheTable cacheTable)
{
_columnKey = columnKey; // UniqueName
_cacheTable = cacheTable; // Cache source
}
public void InstantiateIn(Control container)
{
RadComboBox rcb = new RadComboBox();
rcb.ID = "ddl_" + _columnKey;
rcb.Width = Unit.Pixel(130);
rcb.MarkFirstMatch = true;
rcb.AllowCustomText = false;
rcb.Filter = RadComboBoxFilter.Contains;
rcb.EnableViewState = true;
rcb.DataBinding += (sender, args) =>
{
RadComboBox combo = (RadComboBox)sender;
GridEditableItem item =
(GridEditableItem)combo.NamingContainer;
BindComboBox(combo, _cacheTable);
//Select existing value
string currentValue = item[_columnKey].Text;
if (!string.IsNullOrWhiteSpace(currentValue) &&
currentValue != " ")
{
RadComboBoxItem selected =
combo.Items.FindItemByText(currentValue);
if (selected != null)
{
selected.Selected = true;
}
}
// save
/* combo.Attributes.Add("data-Name", _columnKey);
combo.Attributes.Add(
"data-RowId",
item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString()
);*/
};
container.Controls.Add(rcb);
}
private static void BindComboBox(RadComboBox rcbComboBox, CacheTable cacheTable)
{
List list = ClientUtil.GetMasterData(cacheTable);
rcbComboBox.DataSource = list;
rcbComboBox.DataTextField = "DataTextField";
rcbComboBox.DataValueField = "DataValueField";
rcbComboBox.DataBind();
}
public class AutoCompleteEditTemplate : ITemplate
{
private readonly string _uniqueName;
public AutoCompleteEditTemplate(string uniqueName)
{
_uniqueName = uniqueName;
}
public void InstantiateIn(Control container)
{
RadAutoCompleteBox ac = new RadAutoCompleteBox();
ac.ID = "ac_" + _uniqueName;
ac.Width = Unit.Pixel(90);
ac.InputType = RadAutoCompleteInputType.Text;
ac.Filter = RadAutoCompleteFilter.StartsWith;
ac.MinFilterLength = 1;
// ac.TextSettings.SelectionMode = AutoCompleteSelectionMode.Single;
// ac.AllowCustomText = true;
// ac.MarkFirstMatch = true;
ac.DropDownHeight = Unit.Pixel(150);
ac.DropDownWidth = Unit.Pixel(75);
ac.WebServiceSettings.Path = "../../AutoCompleteDataPage.aspx";
ac.WebServiceSettings.Method = GetWebMethodName();
// Bind existing cell value in edit mode
ac.DataBinding += (s, e) =>
{
RadAutoCompleteBox box = (RadAutoCompleteBox)s;
GridEditableItem item =
(GridEditableItem)box.NamingContainer;
string value = item[_uniqueName].Text;
/* if (!string.IsNullOrWhiteSpace(value) &&
value != " ")
{
box.Entries.Clear();
box.Entries.Add(
new AutoCompleteBoxEntry(value, value)
);
}
*/
//save later
/*box.Attributes.Add("data-Name", _uniqueName);
box.Attributes.Add(
"data-RowId",
item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString()
);*/
};
container.Controls.Add(ac);
}
// MAP COLUMN → WEBMETHOD
private string GetWebMethodName()
{
switch (_uniqueName)
{
case "VIA_Station":
return "GetAirportData";
case "Org":
return "GetOrgCountryDataView";
case "Dest":
return "GetDestCountryDataView";
default:
throw new InvalidOperationException(
"No WebMethod mapped for " + _uniqueName);
}
}
}
public class DatePickerEditTemplate : ITemplate
{
private readonly string _uniqueName;
public DatePickerEditTemplate(string uniqueName)
{
_uniqueName = uniqueName; // Effective_Dt / Expiry_Dt
}
public void InstantiateIn(Control container)
{
RadDatePicker dp = new RadDatePicker();
dp.ID = "dt_" + _uniqueName;
dp.Width = Unit.Pixel(110);
// Date formatting
dp.DateInput.DateFormat = "dd-MMM-yyyy";
dp.DateInput.DisplayDateFormat = "dd-MMM-yyyy";
// dp.DateInput.ReadOnly = true;
dp.Calendar.EnableKeyboardNavigation = true;
// Bind existing value when row enters edit mode
dp.DataBinding += (sender, args) =>
{
RadDatePicker picker = (RadDatePicker)sender;
GridEditableItem item =
(GridEditableItem)picker.NamingContainer;
string text = item[_uniqueName].Text;
if (!string.IsNullOrWhiteSpace(text) &&
text != " " &&
DateTime.TryParse(text, out DateTime date))
{
picker.SelectedDate = date;
}
// save
/*picker.Attributes.Add("data-Name", _uniqueName);
picker.Attributes.Add(
"data-RowId",
item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString()
);*/
};
container.Controls.Add(dp);
}
}
protected void rgIRView_Quarantine_ItemCreated(object sender, GridItemEventArgs e)
{
try
{
if (e.Item is GridHeaderItem)
{
GridHeaderItem item = (GridHeaderItem)e.Item;
CheckBox rtb = new CheckBox();
rtb.ID = "chk1";
rtb.CssClass = "ChkHeader";
rtb.Attributes.Add("onclick", "SelectAllCheckboxes(this);");
TableCell cell = (TableCell)item["InterlineRate_Quarantine_ID"];
cell.Controls.Add(rtb);
}
// ROW CHECKBOX THIS WAS MISSING
if (e.Item is GridDataItem dataItem)
{
CheckBox chkRow = new CheckBox();
chkRow.ID = "chk2";
chkRow.CssClass = "chkRateSheet";
chkRow.EnableViewState = true;
chkRow.Attributes.Add("onclick", "SelectChildCheckBoxes(this);");
dataItem["InterlineRate_Quarantine_ID"].Controls.Add(chkRow);
// TableCell cell = dataItem["InterlineRate_Quarantine_ID"];
// cell.Controls.Clear();
// cell.Controls.Add(chkRow);
if (!(ViewState["IsEditMode"] is bool isEdit) || !isEdit)
return;
if (ViewState["EditIds"] == null)
return;
if (ViewState["EditIds"] != null)
{
List editIds = (List)ViewState["EditIds"];
long rowId = Convert.ToInt64(
dataItem.GetDataKeyValue("InterlineRate_Quarantine_ID"));
if (editIds.Contains(rowId))
{
dataItem.Edit = true; //THIS ENABLES INLINE EDIT
}
}
}
1767856244
Anonymous
Ich versuche, mehrere Zeilen in automatisch generierten Spalten von radgrid inline zu bearbeiten. Unten sind die Methoden zum Erstellen von Spalten und zum Erstellen von Elementen aufgeführt, die ich dafür geschrieben habe. Hier habe ich manuelle Vorlagen erstellt, wie Sie den Code unten einchecken können. Die Bearbeitung, die ich zu erhalten versuche, ist die folgende, aber ich erhalte das automatische Bearbeitungsraster, das von Griddataitem stammt. Und der Code dafür ist [code]item.edit == true [/code] Aus diesem Grund bekomme ich die Änderung, aber [url=viewtopic.php?t=30561]ich möchte[/url] das nicht. [url=viewtopic.php?t=30561]Ich möchte[/url] eine Inline-Bearbeitung; Dafür habe ich die Vorlage durch die Vorlage ersetzt, die ich hier geschrieben habe. Außerdem ist beim Debuggen der Index in „ReplaceWithTemplateColumn -1, da die Spalte keinen Index sammelt. [code]ColumnCreated[/code] und ItemCreated: [code]if (!(e.Column is GridBoundColumn boundCol)) return; string field = boundCol.DataField; // autocomplete if (field == "Org" || field == "Dest" || field == "VIA_Station") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new AutoCompleteEditTemplate(field) ); } else if (field == "Level") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.SRA_Level) ); } else if (field == "Carrier") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.Carrier) ); } else if (field == "Product") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.Product) ); } else if (field == "Rate_UOM") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.RateUOM) ); } else if (field == "Currency") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.Currency) ); } else if (field == "Commodity") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DropDownEditTemplate(field, CacheTable.Commodity) ); } else if (field == "Effective_Dt" || field == "Expiry_Dt") { ReplaceWithTemplateColumn( e, boundCol, new DisplayTextTemplate(field), new DatePickerEditTemplate(field) ); } /* private void ReplaceWithTemplateColumn( GridColumnCreatedEventArgs e, GridBoundColumn oldCol, ITemplate itemTemplate, ITemplate editTemplate) { GridTemplateColumn tpl = new GridTemplateColumn { UniqueName = oldCol.UniqueName, DataField = oldCol.DataField, HeaderText = oldCol.HeaderText, SortExpression = oldCol.SortExpression, ItemTemplate = itemTemplate }; if (editTemplate != null) tpl.EditItemTemplate = editTemplate; tpl.HeaderStyle.Width = oldCol.HeaderStyle.Width; tpl.ItemStyle.Width = oldCol.ItemStyle.Width; e.OwnerTableView.Columns.Remove(oldCol); e.OwnerTableView.Columns.Add(tpl); } */ private void ReplaceWithTemplateColumn( GridColumnCreatedEventArgs e, GridBoundColumn oldColumn, ITemplate itemTemplate, ITemplate editTemplate) { GridTemplateColumn newCol = new GridTemplateColumn(); // Preserve identity newCol.UniqueName = oldColumn.UniqueName; newCol.HeaderText = oldColumn.HeaderText; newCol.SortExpression = oldColumn.SortExpression; newCol.DataField = oldColumn.DataField; // Preserve layout newCol.HeaderStyle.Width = oldColumn.HeaderStyle.Width; newCol.ItemStyle.Width = oldColumn.ItemStyle.Width; newCol.ItemStyle.HorizontalAlign = oldColumn.ItemStyle.HorizontalAlign; // Templates newCol.ItemTemplate = itemTemplate; if (editTemplate != null) newCol.EditItemTemplate = editTemplate; // replace at same index // int index = Array.IndexOf(e.OwnerTableView.RenderColumns, e.Column); int index = e.OwnerTableView.Columns.IndexOf(oldColumn); if (index < 0) { return; // column is not in collection anymore } e.OwnerTableView.Columns.Remove(oldColumn); e.OwnerTableView.Columns.Insert(index, newCol); } public class DisplayTextTemplate : ITemplate { private readonly string _uniqueName; public DisplayTextTemplate(string uniqueName) { _uniqueName = uniqueName; } public void InstantiateIn(Control container) { Literal lbl = new Literal(); lbl.DataBinding += (s, e) => { Literal l = (Literal)s; GridDataItem item = (GridDataItem)l.NamingContainer; string text = item[_uniqueName].Text; l.Text = (!string.IsNullOrWhiteSpace(text) && text != " ") ? text : string.Empty; }; container.Controls.Add(lbl); } } public class DropDownEditTemplate : ITemplate { private readonly string _columnKey; private readonly CacheTable _cacheTable; public DropDownEditTemplate(string columnKey, CacheTable cacheTable) { _columnKey = columnKey; // UniqueName _cacheTable = cacheTable; // Cache source } public void InstantiateIn(Control container) { RadComboBox rcb = new RadComboBox(); rcb.ID = "ddl_" + _columnKey; rcb.Width = Unit.Pixel(130); rcb.MarkFirstMatch = true; rcb.AllowCustomText = false; rcb.Filter = RadComboBoxFilter.Contains; rcb.EnableViewState = true; rcb.DataBinding += (sender, args) => { RadComboBox combo = (RadComboBox)sender; GridEditableItem item = (GridEditableItem)combo.NamingContainer; BindComboBox(combo, _cacheTable); //Select existing value string currentValue = item[_columnKey].Text; if (!string.IsNullOrWhiteSpace(currentValue) && currentValue != " ") { RadComboBoxItem selected = combo.Items.FindItemByText(currentValue); if (selected != null) { selected.Selected = true; } } // save /* combo.Attributes.Add("data-Name", _columnKey); combo.Attributes.Add( "data-RowId", item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString() );*/ }; container.Controls.Add(rcb); } private static void BindComboBox(RadComboBox rcbComboBox, CacheTable cacheTable) { List list = ClientUtil.GetMasterData(cacheTable); rcbComboBox.DataSource = list; rcbComboBox.DataTextField = "DataTextField"; rcbComboBox.DataValueField = "DataValueField"; rcbComboBox.DataBind(); } public class AutoCompleteEditTemplate : ITemplate { private readonly string _uniqueName; public AutoCompleteEditTemplate(string uniqueName) { _uniqueName = uniqueName; } public void InstantiateIn(Control container) { RadAutoCompleteBox ac = new RadAutoCompleteBox(); ac.ID = "ac_" + _uniqueName; ac.Width = Unit.Pixel(90); ac.InputType = RadAutoCompleteInputType.Text; ac.Filter = RadAutoCompleteFilter.StartsWith; ac.MinFilterLength = 1; // ac.TextSettings.SelectionMode = AutoCompleteSelectionMode.Single; // ac.AllowCustomText = true; // ac.MarkFirstMatch = true; ac.DropDownHeight = Unit.Pixel(150); ac.DropDownWidth = Unit.Pixel(75); ac.WebServiceSettings.Path = "../../AutoCompleteDataPage.aspx"; ac.WebServiceSettings.Method = GetWebMethodName(); // Bind existing cell value in edit mode ac.DataBinding += (s, e) => { RadAutoCompleteBox box = (RadAutoCompleteBox)s; GridEditableItem item = (GridEditableItem)box.NamingContainer; string value = item[_uniqueName].Text; /* if (!string.IsNullOrWhiteSpace(value) && value != " ") { box.Entries.Clear(); box.Entries.Add( new AutoCompleteBoxEntry(value, value) ); } */ //save later /*box.Attributes.Add("data-Name", _uniqueName); box.Attributes.Add( "data-RowId", item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString() );*/ }; container.Controls.Add(ac); } // MAP COLUMN → WEBMETHOD private string GetWebMethodName() { switch (_uniqueName) { case "VIA_Station": return "GetAirportData"; case "Org": return "GetOrgCountryDataView"; case "Dest": return "GetDestCountryDataView"; default: throw new InvalidOperationException( "No WebMethod mapped for " + _uniqueName); } } } public class DatePickerEditTemplate : ITemplate { private readonly string _uniqueName; public DatePickerEditTemplate(string uniqueName) { _uniqueName = uniqueName; // Effective_Dt / Expiry_Dt } public void InstantiateIn(Control container) { RadDatePicker dp = new RadDatePicker(); dp.ID = "dt_" + _uniqueName; dp.Width = Unit.Pixel(110); // Date formatting dp.DateInput.DateFormat = "dd-MMM-yyyy"; dp.DateInput.DisplayDateFormat = "dd-MMM-yyyy"; // dp.DateInput.ReadOnly = true; dp.Calendar.EnableKeyboardNavigation = true; // Bind existing value when row enters edit mode dp.DataBinding += (sender, args) => { RadDatePicker picker = (RadDatePicker)sender; GridEditableItem item = (GridEditableItem)picker.NamingContainer; string text = item[_uniqueName].Text; if (!string.IsNullOrWhiteSpace(text) && text != " " && DateTime.TryParse(text, out DateTime date)) { picker.SelectedDate = date; } // save /*picker.Attributes.Add("data-Name", _uniqueName); picker.Attributes.Add( "data-RowId", item.GetDataKeyValue("InterlineRate_Quarantine_ID")?.ToString() );*/ }; container.Controls.Add(dp); } } protected void rgIRView_Quarantine_ItemCreated(object sender, GridItemEventArgs e) { try { if (e.Item is GridHeaderItem) { GridHeaderItem item = (GridHeaderItem)e.Item; CheckBox rtb = new CheckBox(); rtb.ID = "chk1"; rtb.CssClass = "ChkHeader"; rtb.Attributes.Add("onclick", "SelectAllCheckboxes(this);"); TableCell cell = (TableCell)item["InterlineRate_Quarantine_ID"]; cell.Controls.Add(rtb); } // ROW CHECKBOX THIS WAS MISSING if (e.Item is GridDataItem dataItem) { CheckBox chkRow = new CheckBox(); chkRow.ID = "chk2"; chkRow.CssClass = "chkRateSheet"; chkRow.EnableViewState = true; chkRow.Attributes.Add("onclick", "SelectChildCheckBoxes(this);"); dataItem["InterlineRate_Quarantine_ID"].Controls.Add(chkRow); // TableCell cell = dataItem["InterlineRate_Quarantine_ID"]; // cell.Controls.Clear(); // cell.Controls.Add(chkRow); if (!(ViewState["IsEditMode"] is bool isEdit) || !isEdit) return; if (ViewState["EditIds"] == null) return; if (ViewState["EditIds"] != null) { List editIds = (List)ViewState["EditIds"]; long rowId = Convert.ToInt64( dataItem.GetDataKeyValue("InterlineRate_Quarantine_ID")); if (editIds.Contains(rowId)) { dataItem.Edit = true; //THIS ENABLES INLINE EDIT } } } [/code] [code] [/code]