I would like to inject the primary key column dynamically into a grid and set the DataKeyField to it.
If I pre define the primary key column in a UltraWebGrid and set the DataKeyField to it at design time,
and in UpdateCell event if I access e.Cell.Row.DataKey everything is working fine.
If I try to add the primary key column in a UltraWebGrid and set the DataKeyField to it programatically,
e.Cell.Row.DataKey returns null. Is there another way to achive this, I mean using some other lifecycle
events..?
<igtbl:UltraWebGrid ID="uwgDepositList" runat="server" Height="134px" Width="400px" OnInitializeRow="uwgDepositList_InitializeRow" OnUpdateCell="uwgDepositList_UpdateCell" > <Bands> <igtbl:UltraGridBand BaseTableName="DepositItem" AllowUpdate="Yes"> <Columns> <igtbl:UltraGridColumn BaseColumnName="GRID_KEY_ID" IsBound="True" Key="GRID_KEY_ID"> <Header Caption="GRID_KEY_ID"> <RowLayoutColumnInfo OriginX="1" /> </Header> <Footer> <RowLayoutColumnInfo OriginX="1" /> </Footer> </igtbl:UltraGridColumn> <igtbl:UltraGridColumn BaseColumnName="COL_UNBOUND" IsBound="True" Key="COL_UNBOUND"> <Header Caption="COL_UNBOUND"> <RowLayoutColumnInfo OriginX="1" /> </Header> <Footer> <RowLayoutColumnInfo OriginX="1" /> </Footer> </igtbl:UltraGridColumn> </Columns> <RowTemplateStyle BackColor="White" BorderColor="White" BorderStyle="Ridge"> <BorderDetails WidthBottom="3px" WidthLeft="3px" WidthRight="3px" WidthTop="3px" /> </RowTemplateStyle> <AddNewRow View="NotSet" Visible="NotSet"> </AddNewRow> </igtbl:UltraGridBand> </Bands> <DisplayLayout BorderCollapseDefault="Separate" Name="uwgDepositList" RowHeightDefault="20px" StationaryMarginsOutlookGroupBy="True" TableLayout="Fixed" Version="4.00" AllowUpdateDefault="Yes" AutoGenerateColumns="False" CellClickActionDefault="Edit" RowSelectorsDefault="No"> <FrameStyle BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt" Height="134px" Width="400px"> </FrameStyle> <RowAlternateStyleDefault BackColor="#FFFFC0"> <Padding Left="3px" /> <BorderDetails ColorLeft="255, 255, 192" ColorTop="255, 255, 192" /> </RowAlternateStyleDefault> <EditCellStyleDefault BorderStyle="None" BorderWidth="0px"> </EditCellStyleDefault> <FooterStyleDefault BackColor="LightGray" BorderStyle="Solid" BorderWidth="1px"> <BorderDetails ColorLeft="White" ColorTop="White" WidthLeft="1px" WidthTop="1px" /> </FooterStyleDefault> <HeaderStyleDefault BackColor="LightGray" BorderStyle="Solid"> <BorderDetails ColorLeft="White" ColorTop="White" WidthLeft="1px" WidthTop="1px" /> </HeaderStyleDefault> <RowStyleDefault BorderColor="Gray" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt"> <Padding Left="3px" /> <BorderDetails ColorLeft="White" ColorTop="White" /> </RowStyleDefault> <AddNewBox> <BoxStyle BackColor="LightGray" BorderStyle="Solid" BorderWidth="1px"> <BorderDetails ColorLeft="White" ColorTop="White" WidthLeft="1px" WidthTop="1px" /> </BoxStyle> </AddNewBox> <ActivationObject BorderColor="Black" BorderWidth=""> </ActivationObject> <FilterOptionsDefault> <FilterDropDownStyle BackColor="White" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" CustomRules="overflow:auto;" Font-Names="Verdana,Arial,Helvetica,sans-serif" Font-Size="11px" Width="200px"> <Padding Left="2px" /> </FilterDropDownStyle> <FilterHighlightRowStyle BackColor="#151C55" ForeColor="White"> </FilterHighlightRowStyle> <FilterOperandDropDownStyle BackColor="White" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" CustomRules="overflow:auto;" Font-Names="Verdana,Arial,Helvetica,sans-serif" Font-Size="11px"> <Padding Left="2px" /> </FilterOperandDropDownStyle> </FilterOptionsDefault> </DisplayLayout> </igtbl:UltraWebGrid>
private void Page_Load(object sender, EventArgs e){ InitData();}
public void InitData(UltraWebGrid grid) { if (!IsPostBack) {
//grid.Columns.Add(new UltraGridColumn("SEL", "", ColumnType.CheckBox, false)) DataTable oDt = new DataTable("DepositItem"); oDt.Columns.Add(new DataColumn("ROW_KEY", typeof (int))); oDt.Columns.Add(new DataColumn("GRID_KEY_ID", typeof (String))); oDt.Columns.Add(new DataColumn("COL_UNBOUND", typeof (String)));
var row = oDt.NewRow(); row["ROW_KEY"] = 111; row["GRID_KEY_ID"] = "1000"; row["COL_UNBOUND"] = "1000Test"; oDt.Rows.Add(row); row = oDt.NewRow(); row["ROW_KEY"] = 222; row["GRID_KEY_ID"] = "2000"; row["COL_UNBOUND"] = "2000Test"; oDt.Rows.Add(row); grid.DataSource = oDt; grid.DataBind(); } }
protected void uwgDepositList_UpdateCell(object sender, CellEventArgs e) { int i = 0; }
protected void uwgDepositList_InitializeRow(object sender, RowEventArgs e) { }
When a grid's layout is being automaticalyl generated in this fashion, a good practice is to set the DataKeyField property on all of your grid's bands in the InitializeLayout event. This event occurs right after the call to DataBind() is made, and before the row objects are created.
I suspect that your issue is occuring because of how you're adding the column to your grid, assuming that you're doing this in the same way that you add the "SEL" column to your grid (which is commented out in your code snippet):
vinodres said://grid.Columns.Add(new UltraGridColumn("SEL", "", ColumnType.CheckBox, false))
This approach means that the column will not be stored in ViewState. Instead, you can use the following approach:
using Infragistics.WebUI.UltraWebGrid;...// Create the new column so that it is stored in ViewStateUltraGridColumn col = new UltraGridColumn(true);// Set properties on the columncol.Key = "SEL";col.Header.Caption = "";col.Type = ColumnType.Checkbox;col.DefaultValue = false;// Add the column to the collectiongrid.Columns.Add(col);
We are using the WebDataGrid and would also like to build the grid's data columns programatically as we would like to create some generic routines for handling mulitple tables. What I have so far is not quite working. I'm having trouble finding a code example of how to build a simple webdatagrid programmatically, assigning column editor providers, and allowing for adds, updates and deletes through the codebehind. What I have so far mostly works, but I get a DataKeyField issue when try to update a field and then do a postback. Can you provide a simple example of using programmatic building of edit providers that handle numeric, date, boolean, and text fields. This is what I have so far (please see included code below), but I can not get it to update, and realise I am missing at least one thing (maybe two, maybe three?). Thanks a lot for your help!
TestIGGV()
Try
DataSet
ds = MySession.CC.FillDataSet(
, MySession.CC.Conn, MySession.TextValue, MySession.WhereClause)
'== IF DATA COLUMNS EXIST CREATE THE EDITOR PROVIDERS
Then
'== ENABLE CELL EDITING
EditingCore)()
CellEditing)()
'Me.iggv.Behaviors.CreateBehavior(Of Filtering)()
'Me.iggv.Behaviors.EditingCore.Behaviors.CellEditing.EditModeActions.MouseClick = EditMouseClickAction.Single
'== CREATE A DATA TABLE TO PREVENT CREATING THE PROVIDER MULTIPLE TIMES
DataTable
DataColumn
dtProviders.Columns.Add(
))
keys(0) = dtProviders.Columns(
)
'== SET THE PRIMARYKEY PROPERTY OF THE TABLE
dtProviders.PrimaryKey = keys
'== GET THE COLUMN TYPES
DataRow
dr = ds.Tables(0).Rows(0)
Nothing
'== CREATE THE PROVIDERS
ds.Tables(0).Columns.Count - 1
'== HAS A PROVIDER FOR THIS TYPE ALREADY BEEN CREATED
DataRow = dtProviders.Rows.Find(dr.Item(intCounter).ToString)
'== PREPARE A NEW ROW TO HOLD THE PROVIDER ID
= dr.Item(intCounter).GetType.ToString
= ds.Tables(0).Columns(intCounter).ToString
EditingColumnSetting()
DataRow = dtProviders.NewRow()
drProviderName(
) = DataColumnName
'== FOR EACH COLUMN THAT IS NOT NAMED "ID" BUT ENDS IN "ID" GET THE COLUMN NAME AND
' REFERENCE THE TABLE IT IS RELATED TO SO THAT WE CAN CREATE THE DATABOUND CONTROLS AND FILL THEM.
& DataColumnName.ToString.Substring(0, DataColumnName.Length - 2)
& TableName
DataSet = MySession.CC.FillDataSet(StoredProcedureName, MySession.CC.Conn)
DropDownProvider()
ieProvider.ID =
strProviderID = ieProvider.ID
'ieProvider.EditorControl.TextField = "Name"
'ieProvider.EditorControl.ValueField = "ID"
''bert'ieProvider.EditorControl.ID = "EditorControl" & intCounter
''bert'ieProvider.EditorControl.DisplayMode = DropDownDisplayMode.DropDownList
ieProvider.EditorControl.DropDownItemBinding.TextField =
"Name"
ieProvider.EditorControl.DropDownItemBinding.ValueField =
"ID"
ieProvider.EditorControl.DropDownOrientation = DropDownOrientation.Default
ieProvider.EditorControl.DropDownContainerWidth =
"250"
(ieProvider.GetEditor(), WebDropDown)
ddl.DataKeyFields =
ddl.DataSource = dsDDL
ddl.DataBind()
ddl.ID =
ieProvider.GetEditor().ID = DataColumnName
columnSetting.ColumnKey = DataColumnName
columnSetting.EditorID = strProviderID
.iggv.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(columnSetting)
'== USE THE TABLE NAME TO GET THE DROPDOWNLIST DATA
.iggv.EditorProviders.Add(ieProvider)
NumericEditorProvider()
"iep_Number"
'ElseIf IsDate(dr.Item(intCounter)) Then
' Dim ieProvider As DatePickerProvider = New DatePickerProvider()
' ieProvider.ID = "iep_Date"
' strProviderID = ieProvider.ID
' Me.iggv.EditorProviders.Add(ieProvider)
Else
TextBoxProvider
ieProvider.EditorControl.TextMode = TextBoxMode.MultiLine
ieProvider.EditorControl.CssClass =
"igg_EditTextArea"
"iep_Text"
If
'== HOOKS UP THE PROVIDER TO THE COLUMN BY KEY
columnSetting.ColumnKey = ds.Tables(0).Columns(intCounter).ColumnName.ToString
'== ADD THE TYPE TO THE LIST
dtProviders.Rows.Add(drProviderName)
Next
.iggv.DataSource = ds
.iggv.DataBind()
Exception
MySession.CC.HandleMessage(ex.Message,
Sub
Sorry about the spacing on that code example! I have no idea why it did that. I cut and pasted into the window from Visual Studio. :P I don't seem to be able to edit the post, or I would have fixed the spacing myself.