/// <summary> /// Shows the search result in the UI /// </summary> /// <param name="xdata"></param> private void ShowData(agsXMPP.protocol.x.data.Data xdata) { lock (this) { //ClearGridAndDataTable(); dataGridView1.SuspendLayout(); _dataTable.Rows.Clear(); _dataTable.Columns.Clear(); dataGridView1.Columns.Clear(); agsXMPP.protocol.x.data.Reported reported = xdata.Reported; #region GRID COLUMNS if (reported != null) { foreach (agsXMPP.protocol.x.data.Field f in reported.GetFields()) { // Create header DataGridViewTextBoxColumn header = new DataGridViewTextBoxColumn(); header.DataPropertyName = f.Var; header.HeaderText = f.Label; header.Name = f.Var; dataGridView1.Columns.Add(header); // Create dataTable Col _dataTable.Columns.Add(f.Var, typeof(string)); } } #endregion #region GRID ROWS agsXMPP.protocol.x.data.Item[] items = xdata.GetItems(); foreach (agsXMPP.protocol.x.data.Item item in items) { DataRow dataRow = _dataTable.Rows.Add(); foreach (agsXMPP.protocol.x.data.Field field in item.GetFields()) { dataRow[field.Var] = field.GetValue(); } } #endregion if (_dataTable.Rows.Count == 0) { toolStripStatusLabel1.Text = "no items found"; } else { toolStripStatusLabel1.Text = String.Format("{0} items found", _dataTable.Rows.Count.ToString()); } dataGridView1.ResumeLayout(); } }
private void OnSearchResult(object sender, IQ iq, object data) { // We will upate the GUI from here, so invoke if (InvokeRequired) { // Windows Forms are not Thread Safe, we need to invoke this :( // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new IqCB(OnSearchResult), new object[] { sender, iq, data }); return; } /* * Example 9. Service Returns Search Results * * <iq type='result' * from='characters.shakespeare.lit' * to='[email protected]/balcony' * id='search4' * xml:lang='en'> * <query xmlns='jabber:iq:search'> * <x xmlns='jabber:x:data' type='result'> * <field type='hidden' var='FORM_TYPE'> * <value>jabber:iq:search</value> * </field> * <reported> * <field var='first' label='First Name'/> * <field var='last' label='Last Name'/> * <field var='jid' label='Jabber ID'/> * <field var='gender' label='Gender'/> * </reported> * <item> * <field var='first'><value>Benvolio</value></field> * <field var='last'><value>Montague</value></field> * <field var='jid'><value>[email protected]</value></field> * <field var='gender'><value>male</value></field> * </item> * <item> * <field var='first'><value>Romeo</value></field> * <field var='last'><value>Montague</value></field> * <field var='jid'><value>[email protected]</value></field> * <field var='gender'><value>male</value></field> * </item> * </x> * </query> * </iq> */ if (iq.Type == IqType.result) { if (iq.Query is Search) { agsXMPP.protocol.x.data.Data xdata = ((Search)iq.Query).Data; if (xdata != null) { ShowData(xdata); } else { ShowData(iq.Query as Search); } } } else { ClearGridAndDataTable(); toolStripStatusLabel1.Text = "an error occured in the search request"; } }