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

Parents
  • 45049
    Suggested Answer
    posted

    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 ViewState
    UltraGridColumn col = new UltraGridColumn(true);

    // Set properties on the column
    col.Key = "SEL";
    col.Header.Caption = "";
    col.Type = ColumnType.Checkbox;
    col.DefaultValue = false;

    // Add the column to the collection
    grid.Columns.Add(col);

Reply Children