Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
80
How to add DataKeyField programatically
posted

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)
        {
        }