Helping Ideas

Working with UltraGrid

Posted on: April 27, 2009

How to make DropDownList in UltraGrid:

This articles tells how to add ddl in UltraGrid and how to populates ddl dynamically.

About the application:
 
There are four tables in sql.
  • Type
  • Country
  • Currency
  • Language

Type table contains Country,Language,Currency values and an integer code is assigned to each type.

Type-Table

 

Country table contains the following values
CountryTable


Currency table contains the following values.
currency
Language table contains the following values.
language


The grid contains the following columns.

·        Type

·        SubType

·        Conditon

·       RemoveRow

emptygrid1

 

When AddExpression button is clicked one row is displayed in the grid. Type column will be filled by the three types and SubType and Condition column contains empty lists as.

gtype
The list in the SubType column will be populated after the selection from Type list. If user selects Country then Country sub types are displayed in the Sub Type list same for Currency and Language.

gaddexp
When generate Expression is clicked then expression is made as
Country:Asia-China OR Currency:Dollar-AU

s32

Steps to follow:

First of all make a dll to connect to the database. This is the code for GetData.dll 

 using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Data;

namespace GetData

{

    public class GetNews

    {

        public SqlConnection sqlConn;

        public SqlDataAdapter oAdapter;

        public DataSet odataSet;

        public SqlCommand oCommand;

        public DataTable oTable;

        public GetNews()

        {

            try

            {

                sqlConn = new SqlConnection(“Data Source=ssi-aiqbal;Network Library=DBMSSOCN;Initial Catalog=NewsDatabase;User ID=sa;Password=sa;”);

                oAdapter = new SqlDataAdapter();

                odataSet = new DataSet();

                oCommand = new SqlCommand();

                oTable = new DataTable();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        public void OpenConnection()

        {

            try

            {

                sqlConn.Open();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        public void CloseConnection()

        {

            try

            {

                sqlConn.Close();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        public DataSet Select()

        {

            try

            {

                oCommand.CommandText = “select * from News”;

                oCommand.CommandType = CommandType.Text;

                oCommand.Connection = sqlConn;

                oAdapter.SelectCommand = oCommand;

                oAdapter.Fill(odataSet);

                return odataSet;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        public DataSet GetTMCType()

        {

            oCommand.CommandText = “select distinct [Name] from Type”;

            oCommand.CommandType = CommandType.Text;

            oCommand.Connection = sqlConn;

            oAdapter.SelectCommand = oCommand;

            oAdapter.Fill(odataSet);

            return odataSet;

        }

        public DataSet getTCMNameDesc(string Id)

        {

            try

            {

                oCommand.CommandText = “Select Continent,Country,Code from Country”;

 

                if (Id.Equals(“Currency”))

                {

                    oCommand.CommandText = “Select Currency,Country,Code from Currency”;

                }

                else if (Id.Equals(“Language”))

                {

                    oCommand.CommandText = “Select Lang,Dilect,Code from Language”;

                }

                oCommand.CommandType = CommandType.Text;

                oCommand.Connection = sqlConn;

                oAdapter.SelectCommand = oCommand;

                oAdapter.Fill(odataSet);

                odataSet.Tables.Add(oTable);

                oAdapter.Update(oTable);

                return odataSet;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

    }

 

}
 
 
 
 
 
 
 
 

 

  1. Start a new Windows Application in Visual Studio.
  2. Drag a UltraGrid on the Form
  3. Add a reference of GetData.dll in application
  4. Make a function IntializeValueLists and add three lists in ValueList property of UltraGrid.
  5. Initialize the TypeList by calling the GetData. GetTMCType() function.
  6. Make a function CreateNewsExp and build a dataTable having four columns Type, Sub Type,Condition,Remove.
  7. Call the above two function in Form_Load event.
  8. Handle the buttons click events to perform the above explained functionality.
  9. Handle grid InitializeLayout event to set the style and valuelist property of columns.
  10. Handle grid AfterCellListCloseUp event to fill the Sub Type ValueList.
  11. Handle Grid ClickCellButton event to remove the active row on clicking last column i-e Remove in a grid
  12. Handle Grid CellListSelect event to show empty text in the next cell if a value is selected from the Type drop down.

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using GetData;

using System.Collections;

using System.Data.SqlClient;

using Infragistics.Win;

using Infragistics.Win.UltraWinGrid;

namespace SampleGrid

{

    public partial class Form1 : Form

    {

        DataTable dataTable = new DataTable(“TableExp”);

        public GetNews newsObj;

        ArrayList TMCNameDiscAL = new ArrayList();

        string prvType = “”;

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            IntializeValueLists();

            this.ultraGrid1.DataSource = CreateNewsExp();

        }

        private DataTable CreateNewsExp()

        {

            DataColumn colWork = new DataColumn(“Type”, typeof(string));

            dataTable.Columns.Add(colWork);

            colWork = new DataColumn(“Sub Type”, typeof(string));

            dataTable.Columns.Add(colWork);

            colWork = new DataColumn(“Condition”, typeof(string));

            dataTable.Columns.Add(colWork);

            colWork = new DataColumn(“Remove Row”, typeof(string));

            dataTable.Columns.Add(colWork);

            return dataTable;

        }

        private void btnAddExp_Click(object sender, EventArgs e)

        {

            DataRow row = dataTable.NewRow();

            if (dataTable.Rows.Count != 0)

                dataTable.Rows[dataTable.Rows.Count - 1]["Condition"] = “OR”;

            row["Condition"] = “”;

            row["Type"] = “Commodity”;

            row["Sub Type"] = “”;

            row["Remove Row"] = “”;

            dataTable.Rows.Add(row);

            this.ultraGrid1.DataSource = dataTable;

        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)

        {

            e.Layout.Bands[0].Columns[0].Style = (Infragistics.Win.UltraWinGrid.ColumnStyle)6;

            e.Layout.Bands[0].Columns[0].ValueList = e.Layout.ValueLists["TMCTypes"];

 

            e.Layout.Bands[0].Columns[1].Style = (Infragistics.Win.UltraWinGrid.ColumnStyle)6;

            e.Layout.Bands[0].Columns[1].ValueList = e.Layout.ValueLists["TMCNames"];

 

            e.Layout.Bands[0].Columns[2].Style = (Infragistics.Win.UltraWinGrid.ColumnStyle)6;

            e.Layout.Bands[0].Columns[2].ValueList = e.Layout.ValueLists["ANDOR"];

            e.Layout.Bands[0].Columns[2].Header.ToolTipText = “TMC Condition”;

           

            e.Layout.Bands[0].Columns[3].Style = (Infragistics.Win.UltraWinGrid.ColumnStyle)8;

            e.Layout.Bands[0].Columns[3].Header.ToolTipText = “Remove Row”;

           

            Infragistics.Win.UltraWinGrid.ButtonDisplayStyle btnStyle = (Infragistics.Win.UltraWinGrid.ButtonDisplayStyle)3;

            foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn col in this.ultraGrid1.DisplayLayout.Bands[0].Columns)

                col.ButtonDisplayStyle = btnStyle;

 

            int totalWidth = ultraGrid1.Width – 110;

            if (ultraGrid1.DisplayLayout.Bands[0].Columns.Count != 0)

            {

                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Width = totalWidth / 2;

                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Width = totalWidth / 2;

                ultraGrid1.DisplayLayout.Bands[0].Columns[2].Width = 58;

                ultraGrid1.DisplayLayout.Bands[0].Columns[3].Width = 50;

            }          

        }

        private void IntializeValueLists()

        {

            Infragistics.Win.ValueList ANDORValueList = this.ultraGrid1.DisplayLayout.ValueLists.Add(“ANDOR”);

            ANDORValueList.ValueListItems.Add(“AND”);

            ANDORValueList.ValueListItems.Add(“OR”);

            Infragistics.Win.ValueList TMCNamesValueList = this.ultraGrid1.DisplayLayout.ValueLists.Add(“TMCNames”);

            Infragistics.Win.ValueList TMCTypesValueList = this.ultraGrid1.DisplayLayout.ValueLists.Add(“TMCTypes”);

            try

            {

                TMCTypesValueList.ValueListItems.Clear();

                if (newsObj == null)

                    newsObj = new GetNews();

                newsObj.OpenConnection();

                DataTable dt  = newsObj.GetTMCType().Tables[0]; // Fill type list

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    TMCTypesValueList.ValueListItems.Add(dt.Rows[i][0]);

                }

            }

            catch (NullReferenceException nre)

            {

                MessageBox.Show(nre.ToString());

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void BuildArrayList(string strType)

        {

 

            Infragistics.Win.ValueList TMCNamesValueList = this.ultraGrid1.DisplayLayout.ValueLists["TMCNames"];

            try

            {

                TMCNameDiscAL = new ArrayList();

                newsObj = new GetNews();

                DataTable dt=newsObj.getTCMNameDesc(strType).Tables[0];

 

                for (int i = 0; i <dt.Rows.Count ; i++)

                {

                    TMCNameDesc obj = new TMCNameDesc();

                    obj.szType = dt.Rows[i][0].ToString();

                    obj.szSubType = dt.Rows[i][1].ToString();

                    obj.szCode = dt.Rows[i][2].ToString();

                    TMCNameDiscAL.Add(obj);

                }

                TMCNameDiscAL.Sort();

                // Fill the Combo Box

                TMCNamesValueList.ValueListItems.Clear();

                foreach (TMCNameDesc objNames in TMCNameDiscAL)

                {

                    TMCNamesValueList.ValueListItems.Add(objNames.szType+“-“+objNames.szSubType);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void btnResetGrid_Click(object sender, EventArgs e)

        {

            try

 

                dataTable.Rows.Clear();

                dataTable.AcceptChanges();

                this.ultraGrid1.DataSource = dataTable;

                this.textBox1.Text = “”;

 

            }

            catch (Exception ex)

            {

                throw (ex);

            }

        }

        private void ultraGrid1_KeyPress(object sender, KeyPressEventArgs e)

        {

            UltraGrid ultraGrid = sender as UltraGrid;

            UltraGridCell activeCell = ultraGrid == null ? null : ultraGrid.ActiveCell;

            try

            {

                //now check if there is an active cell and if it is in edit mode, can it enter the edit mode

                if (activeCell != null && activeCell.IsInEditMode == false && activeCell.CanEnterEditMode)

                {

                    //now check if the character is not a control character

                    if (char.IsControl(e.KeyChar) == false)

                    {

                        //try to put grid in edit mode

                        ultraGrid.PerformAction(UltraGridAction.EnterEditMode);

                        //if the cell is an Actice Cell and is already in Edit mode

                        if (ultraGrid1.ActiveCell == activeCell && activeCell.IsInEditMode)

                        {

                            //get the cell editor

                            EmbeddableEditorBase CellEditor = activeCell.EditorResolved;

                            //if the editor supports selectable text

                            if (CellEditor.SupportsSelectableText)

                            {

                                //select all the text so that it could be replaced

                                CellEditor.SelectionStart = 0;

                                CellEditor.SelectionLength = CellEditor.TextLength;

                                if (CellEditor is EditorWithMask)

                                {

                                    CellEditor.SelectedText = string.Empty;

                                }

                                else

                                {

                                    //then replace the selected text with the new character

                                    CellEditor.SelectedText = new string(e.KeyChar, 1);

                                    //now mark the event as handled, so that the grid does not handle it

                                    e.Handled = true;

                                }

                            }

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                 MessageBox.Show(ex.ToString());

            }

        }

        private void ultraGrid1_AfterCellListCloseUp(object sender, CellEventArgs e)

        {

            if (e.Cell.Column.Index == 0)  // if Type Dropdwonlist 

            {

                try

                {

                    if ((ultraGrid1.Rows.Count != 0) && (ultraGrid1.ActiveRow != null))

                    {

                        if (ultraGrid1.ActiveRow.Cells[0].Text != prvType)

                        {

                            BuildArrayList(ultraGrid1.ActiveRow.Cells[0].Text);

                        }

 

                        prvType = ultraGrid1.ActiveRow.Cells[0].Text;

                        Application.DoEvents();

                    }

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message);

                }

            }

            if (e.Cell.Column.Index == 2) // if Condition Dropdwonlist, Empty List for last row 

            {

                if ((ultraGrid1.Rows.Count != 0) && (ultraGrid1.ActiveRow != null))

                {

                    if (e.Cell.Row.Index == ultraGrid1.Rows.Count – 1)

                    {

                        Infragistics.Win.ValueList ANDORValueList = this.ultraGrid1.DisplayLayout.ValueLists["ANDOR"];

                        ANDORValueList.ValueListItems.Clear();

                    }

                    else

                    {

                        Infragistics.Win.ValueList ANDORValueList = this.ultraGrid1.DisplayLayout.ValueLists["ANDOR"];

                        if (ANDORValueList.ValueListItems.Count < 2)

                        {

                            ANDORValueList.ValueListItems.Clear();

                            ANDORValueList.ValueListItems.Add(“OR”);

                            ANDORValueList.ValueListItems.Add(“AND”);

                        }

                    }

                }

            }

        }

        private void ultraGrid1_ClickCellButton(object sender, CellEventArgs e)

        {

            try

            {

                if (ultraGrid1.ActiveRow != null)

                {

                    ultraGrid1.SuspendLayout();

                    ultraGrid1.Enabled = false;

                    if (e.Cell.Column.Index == 3)

                    {

                        dataTable.Rows.RemoveAt(e.Cell.Row.Index);

                        if (dataTable.Rows.Count != 0)

                            dataTable.Rows[dataTable.Rows.Count - 1]["Condition"] = “”;

                        dataTable.AcceptChanges();

                        this.ultraGrid1.Refresh();

                        Application.DoEvents();

                    }

                    ultraGrid1.Enabled = true;

                    ultraGrid1.ResumeLayout();

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            finally

            {

                ultraGrid1.Enabled = true;

            }

        }

        private void ultraGrid1_CellListSelect(object sender, CellEventArgs e)

        {

            if (e.Cell.Column.Index == 0)

            {

                e.Cell.Row.Cells[1].Value = “”;

            }

        }

        private void ultraGrid1_Resize(object sender, EventArgs e)

        {

            int colwidths = 110;

            if (ultraGrid1.Width > 150)

            {

                int totalWidth = ultraGrid1.Width – colwidths;

                if (ultraGrid1.DisplayLayout.Bands[0].Columns.Count != 0)

                {

                    ultraGrid1.DisplayLayout.Bands[0].Columns[0].Width = totalWidth / 2;

                    ultraGrid1.DisplayLayout.Bands[0].Columns[1].Width = totalWidth / 2;

                    ultraGrid1.DisplayLayout.Bands[0].Columns[2].Width = colwidths – 52;

                    ultraGrid1.DisplayLayout.Bands[0].Columns[3].Width = colwidths – 80;

                }

            }

        }

        private void btnGenExp_Click(object sender, EventArgs e)

        {

            try

            {

                string szExpression = string.Empty;

                if (ultraGrid1.Rows.Count != 0)

                {

                    for (int i = 0; i < ultraGrid1.Rows.Count; i++)

                    {

                        szExpression += ultraGrid1.Rows[i].Cells[0].Text.Trim() + “:” + ultraGrid1.Rows[i].Cells[1].Text.Trim()+” “ ;

                        if (ultraGrid1.Rows[i].Cells[2].Text == “OR”)

                            szExpression += “OR “;

                        else if (ultraGrid1.Rows[i].Cells[2].Text == “AND”)

                            szExpression += “AND “;

                    }

                }

                textBox1.Text = szExpression;

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

    }

 

 

 

   

public class TMCNameDesc : IComparable

    {

        public string szType;

        public string szSubType;

        public string szCode;

        public int CompareTo(object obj)

        {

            TMCNameDesc Compare = (TMCNameDesc)obj;

            int result = this.szCode.CompareTo(Compare.szCode);

            if (result == 0)

                result = this.szCode.CompareTo(Compare.szCode);

            return result;

        }

    }

}

 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: