Hi Everyone,
First of all I have to say sorry about my English. It's not my parent language.
Let's focus on the problem I have now. I have a rad grid with the editform. I am sure that I did follow the example.
Here are the codes I have done.
Design code in my user control.
And below is my code behind.
Just in case you guys want to know what kind of datatype that I'm binding it to the grid. It's the arraylist.
Could you please tell where in the code that I miss ? Right now when I click on the "Insert new record " It show me the edit form . But once I click on the update button . There is nothing happen.
Thank you so much.
Dang
First of all I have to say sorry about my English. It's not my parent language.
Let's focus on the problem I have now. I have a rad grid with the editform. I am sure that I did follow the example.
Here are the codes I have done.
Design code in my user control.
<
telerik:RadGrid
ID
=
"RadGrid1"
AllowAutomaticUpdates
=
"True"
runat
=
"server"
Width
=
"100%"
GridLines
=
"None"
AutoGenerateColumns
=
"False"
PageSize
=
"13"
AllowSorting
=
"True"
AllowPaging
=
"True"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
ShowStatusBar
=
"True"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnDeleteCommand
=
"RadGrid1_DeleteCommand"
AllowFilteringByColumn
=
"True"
CellSpacing
=
"0"
onitemdatabound
=
"RadGrid1_ItemDataBound"
>
<
MasterTableView
DataKeyNames
=
"ID"
AllowMultiColumnSorting
=
"True"
Width
=
"100%"
CommandItemDisplay
=
"Top"
InsertItemDisplay
=
"Top"
CommandItemSettings-AddNewRecordText
=
"Add new record"
HierarchyLoadMode
=
"ServerBind"
>
<
CommandItemSettings
AddNewRecordText
=
"Add new record"
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
FilterControlAltText
=
"Filter RowIndicator column"
></
RowIndicatorColumn
>
<
ExpandCollapseColumn
FilterControlAltText
=
"Filter ExpandColumn column"
></
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
EditImageUrl
=
"~/RadControls/Grid/Skins/Edit.gif"
UniqueName
=
"EditCommandColumn"
>
<
HeaderStyle
Width
=
"16px"
></
HeaderStyle
>
<
ItemStyle
Width
=
"16px"
/>
</
telerik:GridEditCommandColumn
>
<
telerik:GridButtonColumn
ConfirmText
=
"Are you sure you want to delete this record?"
UniqueName
=
"DeleteColumn"
CommandName
=
"Delete"
ButtonType
=
"ImageButton"
ImageUrl
=
"~/RadControls/Grid/Skins/Delete.gif"
Text
=
"Delete"
>
<
HeaderStyle
Width
=
"16px"
></
HeaderStyle
>
<
ItemStyle
Width
=
"16px"
/></
telerik:GridButtonColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"Name"
SortExpression
=
"Name"
HeaderText
=
"Document Name"
DataField
=
"Name"
/>
<
telerik:GridBoundColumn
UniqueName
=
"Description"
HeaderText
=
"Description"
Visible
=
"false"
DataField
=
"Description"
/>
<
telerik:GridDropDownColumn
UniqueName
=
"DocumentType"
ListDataMember
=
"DocumentType"
SortExpression
=
"DocumentType"
ListTextField
=
"DocumentType"
ListValueField
=
"DocumentType"
HeaderText
=
"Document type"
DataField
=
"DocumentType"
>
</
telerik:GridDropDownColumn
>
<
telerik:GridBoundColumn
datafield
=
"UpdateTime"
AllowFiltering
=
"false"
DataFormatString
=
"{0:dd MMM yyyy HH:mm:ss}"
editformheadertextformat
=
"{0}"
headertext
=
"Last Updated"
readonly
=
"True"
uniquename
=
"UpdateTime"
>
<
ItemStyle
HorizontalAlign
=
"Left"
Width
=
"160px"
Wrap
=
"False"
/>
<
HeaderStyle
HorizontalAlign
=
"Left"
Width
=
"160px"
Wrap
=
"False"
/>
</
telerik:GridBoundColumn
>
</
Columns
>
<
EditFormSettings
CaptionFormatString
=
"Edit record : {0}"
CaptionDataField
=
"Name"
>
<
FormTableItemStyle
Width
=
"100%"
Height
=
"29px"
></
FormTableItemStyle
>
<
FormTableStyle
GridLines
=
"None"
CellSpacing
=
"0"
CellPadding
=
"2"
></
FormTableStyle
>
<
FormStyle
Width
=
"100%"
BackColor
=
"#eef2ea"
></
FormStyle
>
<
EditColumn
ButtonType
=
"ImageButton"
/>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
<
HeaderContextMenu
CssClass
=
"GridContextMenu GridContextMenu_Default"
></
HeaderContextMenu
>
</
telerik:RadGrid
>
And below is my code behind.
protected void Page_Load(object sender, EventArgs e)
{
}
public void Initialize(int iProjectID)
{
RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms;
this.hdProjectID.Value = iProjectID.ToString();
this.RadGrid1.DataBind();
}
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
GridEditManager editMan = editedItem.EditManager;
foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
{
if (column is IGridEditableColumn)
{
IGridEditableColumn editableCol = (column as IGridEditableColumn);
if (editableCol.IsEditable)
{
IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
string editorType = editor.ToString();
string editorText = "unknown";
object editorValue = null;
if (editor is GridTextColumnEditor)
{
editorText = (editor as GridTextColumnEditor).Text;
editorValue = (editor as GridTextColumnEditor).Text;
}
if (editor is GridDropDownColumnEditor)
{
editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +
(editor as GridDropDownColumnEditor).SelectedValue;
editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
}
try
{
int iDataID = (int)editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"];
Data oData = new Data(iDataID, this.UserID);
bool bUpdated = false;
switch(column.UniqueName)
{
case "Name":
oData.Name = editorValue.ToString();
bUpdated = true;
break;
case "Description":
oData.Description = editorValue.ToString();
bUpdated = true;
break;
case "DataType":
oData.TypeID = Convert.ToInt32(editorValue.ToString());
break;
}
if (bUpdated)
{
//
}
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("<
strong
>Unable to update'" + column.HeaderText + "' value.</
strong
> - " + ex.Message));
e.Canceled = true;
break;
}
}
}
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
this.RadGrid1.DataSource = oDatas;
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
switch (e.CommandName)
{
case "":
if (RadGrid1.EditIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.EditItems)
{
GridEditableItem itemToEdit =
(item.OwnerTableView.EditMode == GridEditMode.InPlace) ?
(GridEditableItem)item : (GridEditableItem)item.EditFormItem;
//UpdateItem(itemToEdit);
}
e.Item.OwnerTableView.Rebind();
break;
}
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
GridEditManager editMan = editedItem.EditManager;
Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
Data oDoc = oDatas.Add(1);
foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
{
if (column is IGridEditableColumn)
{
IGridEditableColumn editableCol = (column as IGridEditableColumn);
if (editableCol.IsEditable)
{
IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
string editorText = "unknown";
object editorValue = null;
if (editor is GridTextColumnEditor)
{
editorText = (editor as GridTextColumnEditor).Text;
editorValue = (editor as GridTextColumnEditor).Text;
}
if (editor is GridDropDownColumnEditor)
{
editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +
(editor as GridDropDownColumnEditor).SelectedValue;
editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
}
try
{
switch (column.UniqueName)
{
case "Name":
oDoc.Name = editorValue.ToString();
break;
case "Description":
break;
case "DataType":
break;
}
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to add new Data. Reason: " + ex.Message));
e.Canceled = true;
}
}
}
}
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
GridEditManager editMan = editedItem.EditManager;
GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("DataType"));
DropDownList ddList = editor.DropDownListControl;
DataTypeList oDTList = new DataTypeList();
ddList.DataSource = oDTList;
ddList.DataBind();
}
}
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
int ID = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
oDatas.Remove(ID);
}
Just in case you guys want to know what kind of datatype that I'm binding it to the grid. It's the arraylist.
Could you please tell where in the code that I miss ? Right now when I click on the "Insert new record " It show me the edit form . But once I click on the update button . There is nothing happen.
Thank you so much.
Dang