I have the method here below. Before this method, I was getting the data from one table and now from two tables so I changed ListView as shown below.
Before using two different tables the ListView was designed at properties tab and all was working good.
The method below when writing items bypass the first column and begins from the second.
What is the bug ?
Any help will be very precious.
private void ShowPage()
{
// some declarations such count,LineNbr etc...
if (PublicVariables.PrintData == 1)
{
// seeting column headers and with and alignement if PrintData=1
newtmp = new string[5];
}
else
{
// seeting column headers and with and alignement if PrintData=2
newtmp = new string[7];
}
LineNbr = File.ReadAllLines(fName).Length;
ppc.View = View.Details;
ListViewItem DispItem = new ListViewItem();
while (counter < LineNbr && (line = streamToPrint.ReadLine()) != null)
{
string[] tmp = line.Split('|'); // Splitting the Data
sayac = 0;
for (int i = 0; i < tmp.Length; ++i)
{
if (tmp[i] != "")
{
newtmp[sayac] = tmp[i];
++sayac;
}
}
for (int a=0; a<newtmp.Length; ++a) // I add to SubItems
DispItem.SubItems.Add(newtmp[a]);
ppc.Items.AddRange(new ListViewItem[] {DispItem}); // I pass to ListView ppc
if (PublicVariables.PrintData == 1) //Initialise newtmp string
newtmp = new string[5];
else
newtmp = new string[7];
DispItem = new ListViewItem(); // Initialiase ListViewItem
++counter;
}
}
for (int a=0; a<newtmp.Length; ++a) // I add to SubItems
DispItem.SubItems.Add(newtmp[a]);
ppc.Items.AddRange(new ListViewItem[] {DispItem}); // I pass to Lis
Instead of those lines above, I have to do as below :(
DispItem = new ListViewItem(newtmp);
ppc.Items.Add(DispItem);
I was trying everything to resolve this.
After all, my excuses for those who give mind
Related
I'm trying to automate testing of excel add-in that allows you to create some analysis entities based on pivot table definition.
I'm able to create random pivot table using cubefield objects(I'm adding random fields to rows,columns, and measures) but I need also to add some filtering and here I'm stuck.
Is there any way to get cubefield item list ? For example when I have date field in filter
list of all days and then I want to select some random days for filtering.
This is my code maybe it's not elegant but it works for my purpose.
I'm basically grabbing cubefields check if it's measure or dimension and then I'm setting
it's orientation.
public void PivotTableFieldList()
{
_currentPivotTable = (Excel.PivotTable) _worksheet.PivotTables("PivotTable1");
_currentPivotTable.ManualUpdate = true;
List<Excel.CubeField> measureList = new List<Excel.CubeField>();
List<Excel.CubeField> dimensionList = new List<Excel.CubeField>();
foreach (Excel.CubeField field in _currentPivotTable.CubeFields)
{
if (field.CubeFieldType == Excel.XlCubeFieldType.xlMeasure && field.ShowInFieldList)
{
measureList.Add(field);
}
}
foreach (Excel.CubeField field in _currentPivotTable.CubeFields)
{
if (field.CubeFieldType != Excel.XlCubeFieldType.xlMeasure && field.ShowInFieldList)
{
dimensionList.Add(field);
}
}
Random dimRan=new Random();
dimRan.Next(0, dimensionList.Count);
Random mesRan = new Random();
mesRan.Next(0, measureList.Count);
dimensionList.ToArray();
measureList.ToArray();
Excel.CubeField[] measureRandomList= new Excel.CubeField[3];
Excel.CubeField[] dimensionRandomList = new Excel.CubeField[6];
for (int i = 0; i < 3; i++)
{
measureRandomList[i] = measureList[mesRan.Next(1, measureList.Count)];
}
for (int i = 0; i < 6; i++)
{
dimensionRandomList[i] = dimensionList[dimRan.Next(i, dimensionList.Count)];
}
for (int i = 0; i < 3; i++)
{
var field = measureRandomList[i];
var name = measureRandomList[i].Name;
field.Orientation= Excel.XlPivotFieldOrientation.xlDataField;
}
dimensionRandomList[0].Orientation= Excel.XlPivotFieldOrientation.xlRowField;
dimensionRandomList[1].Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
dimensionRandomList[2].Orientation = Excel.XlPivotFieldOrientation.xlPageField;
dimensionRandomList[2].EnableMultiplePageItems=true;
//How can I get cubefield items list ?
}
You can filter your fields like this:
yourPivotField.CurrentPageName = string.Format("[{0}].[{1}].&[{2}]", SheetName, FieldName, FieldValue);
This sets the filter of your pivot table
Not sure if it it this you were searching for.
On runtime I am changing some columns of datagridview into combobox columns. Now how do I get the existing distinct values in the combobox items? I am using entity model as datasource. My code is:
dgvLoadTable.DataSource = null;
var context = new AdminEntities();
var TableName = cboSelectTable.Text.ToString();
var rawData = context.GetType().GetProperty(TableName).GetValue(context, null);
var truncatedData = ((IQueryable<object>)rawData).Take(0);
var source = new BindingSource { DataSource = truncatedData };
dgvLoadTable.DataSource = source;
dgvLoadTable.ReadOnly = false;
dgvLoadTable.AllowUserToAddRows = true;
for (int row= 0; row < dgvLoadTable.Rows.Count; row++)
{
for (int col = 0; col < dgvLoadTable.Columns.Count; col++)
{
if (col == 2 || col == 4)
{
this.dgvLoadTable[col, row] = new DataGridViewComboBoxCell();
//var ss = dgvLoadTable.AsEnumerable().Select(_ => _.Field<string>(Columns[col])).Distinct();
}
}
}
dgvLoadTable.Refresh();
I'll assume that your code to create the list of values is working..
You need to load those values into the Items of a DataGridViewComboBoxCell; here is code that does that with a list of values:
if (col == 2 || col == 4)
{
var temp = this.dgvLoadTable[col, row].Value;
this.dgvLoadTable[col, row] = new DataGridViewComboBoxCell();
this.dgvLoadTable[col, row].Value = temp;
// if the value lists depend on the column, you may want to create them here!
((DataGridViewComboBoxCell)dgvLoadTable[col, row]).Items.AddRange(numbers.ToArray());
}
Note that for this we must cast the cell to a DataGridViewComboBoxCell as a normal cell doesn't have Items.
In my test case I have used a
List<string> values
and controlled the datatype of my column like this..:
dgvLoadTable[col, row].ValueType = typeof(string);
..beteween the two lines above, ie after creating and before filling the combobox.
Update: I have added two lines to save and restore the cells' values, as these get lost when changing the cells..
I have a GridView with CheckBoxes and I wish to retrieve Cell[1] in every row that was checked. The list always end up being 'null'. The code is below. I added a string to display the output and that works fine. So I'm probably Adding it incorrectly but I don't know what. Any help would be appreciated. Cheers~
List<int> indices = new List<int>();
CheckBox cb = new CheckBox();
string text = "";
foreach (GridViewRow row in GV0.Rows)
{
if (((CheckBox)row.FindControl("CheckBox1")).Checked)
{
text += row.Cells[1].Text;
indices.Add(int.Parse(row.Cells[1].Text));
}
}
Label1.Text = text;
Session["indicesList"] = indices;
Response.Redirect("About.aspx");
The code for the page that is being redirected to:
List<List<string>> all = new List<List<string>>();
List<string> fields = new List<string>();
List<Type> fieldtypes = new List<Type>();
List<int> indices = new List<int>();
int show = 0;
if (!Page.IsPostBack)
{
all = (List<List<string>>)Session["all"];
fields = (List<string>)Session["fields"];
fieldtypes = (List<Type>)Session["fieldtypes"];
indices = (List<int>)Session["indiceslist"];
show = (int)Session["show"];
}
int j = 0;
List<List<string>> dupes = new List<List<string>>();
for (int i = 0; i < show; i++)
{
if (j < indices.Count)
{
if (int.Parse(all[i][0]) == indices[j])
{
dupes.Add(all[i]);
j++;
}
}
}
You're setting your list in the session with a key of indicesList but you're retrieving it with a key of indiceslist (Note the difference in case on the letter "L").
I would suggest creating a property for your list that gets and sets from the session. It makes it much easier to manage.
public List<int> Indices
{
get
{
var val = Session["indicesList"] as List<int>;
if(val == null)
{
val = new List<int>();
Session["indicesList"] = val;
}
return val;
}
set
{
Session["indicesList"] = value;
}
}
This is my sorting logic alphabetic wise
string selectedVal = lstSelectionTags.SelectedValue;
SortedList sortedItems = new SortedList();
for (int i = 0; i < lstSelectionTags.Items.Count; i++)
{
sortedItems.Add(lstSelectionTags.Items[i].Text, lstSelectionTags.Items[i].Value);
}
lstSelectionTags.Items.Clear();
lstSelectionTags.DataSource = sortedItems;
lstSelectionTags.DataTextField = "key";
lstSelectionTags.DataValueField = "value";
lstSelectionTags.DataBind();
when i display items first time in my Listbox by using the below give code
string valueField = Convert.ToString(lstSelectionSub.SelectedItem);
int catID = Convert.ToInt32(lstSelectionSub.SelectedValue);
util = new Utilities();
dt1 = util.GetSubTags_PD(catID, false);
string[] lines = new string[100];
List<string> lines1 = new List<string>();
for (int i = 0; i < dt1.Rows.Count; i++)
{
string s1 = dt1.Rows[i][0].ToString();
if (s1 != "")
{
lines = Regex.Split(s1, ",");
if (!lines1.Contains(lines.ToString()))
{
lines1.AddRange(lines);
}
}
}
lstSelectionTags.DataSource = lines1.Distinct();
lstSelectionTags.DataBind();
It works fine initially and displays the Data but when i do sorting and then try and access the values i don't get any value in the ListBox
EDIT: There seems to be some issue with sorting after sorting the Listbox has a key and value as the DataValue and DataText Field whereas when i rebind it there is no DataValue and DataText Field. Please help.
ISSUE SOLVED : Just used
if (lines1.Count > 0)
{
lstSelectionTags.DataSource = null;
lstSelectionTags.Items.Clear();
lstSelectionTags.DataSource = lines1.Distinct();
lstSelectionTags.DataTextField = null;
lstSelectionTags.DataValueField = null;
lstSelectionTags.DataBind();
}
The code below lets me show emails received in a listview on when the selected index is changed displays the body of the selected email in a RTB. The problem is i changed the code to work with a data grid view and now the Tag part wont work
void SomeFunc() // This line added by Jon
{
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
ListViewItem itmp = new ListViewItem(email.From);
ListViewItem.ListViewSubItem itms1 =
new ListViewItem.ListViewSubItem(itmp, email.Subject);
ListViewItem.ListViewSubItem itms2 =
new ListViewItem.ListViewSubItem(itmp, email.FromName);
itmp.SubItems.Add(itms1);
itmp.SubItems.Add(itms2);
listView1.Items.Add(itmp).Tag = i;
richTextBox1.Text = email.Body;
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
}
private void listView1_SelectionChanged(object sender, EventArgs e)
{
if (listView1.SelectedCells.Count > 0)
{
// bundle is now accessible in your event handler:
richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedCells[0].Tag).Body;
}
}
Code for data grid view
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
string[] row = new string[] { email.From, email.Subject, email.FromName };
object[] rows = new object[] { row };
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
} // This line added by Jon
i have created earlier the code for datagrid view but you already done it so i haven't posted in your last question but i think , you should give a try to the below code.
// i am creating a new object here but , you can have a single object on the form
DataGridView dgv = new DataGridView();
private DataTable EmailSource { get; set; }
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.SelectionChanged+=new EventHandler(dgv_SelectionChanged);
Chilkat.MessageSet msgSet = imap.Search("ALL", true);
if (msgSet != null)
{
bundle = imap.FetchBundle(msgSet);
CreateDataTable();
if (bundle != null && dt!=null)
{
Chilkat.Email email;
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
if(email!=null)
{
DataRow drow = EmailSource.NewRow();
drow["Id"] = i.ToString();
drow["From"] = email.FromName;
drow["Subject"] = email.Subject;
drow["DateRecived"] = email.DateRecived;
// i am adding email body also
drow["Body"] =email.Body;
EmailSource.Rows.Add(drow);
}
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
dgv.DataSource= EmailSource;
// Hiding Body from the grid
dgv.Columns["Body"].Visible =false;
}
}
// this event handler will show the last selected email.
void dgv_SelectionChanged(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
if (rows != null)
{
// get the last selected row
DataRow drow = rows[rows.Count - 1].DataBoundItem as DataRow;
if (drow != null)
{
richTextBox1.Text = drow["Body"];
}
}
}
private void CreateDataTable()
{
EmailSource = new DataTable();
EmailSource.Columns.Add("Id");
EmailSource.Columns.Add("From");
EmailSource.Columns.Add("Subject");
EmailSource.Columns.Add("DateRecived");
EmailSource.Columns.Add("Body");
}
You are adding rows using listView1.Rows.Add(rowArray) in both the code listings. Is that a typo or you named the GridView like that.
Basically, you are storing the index of the email in the "Tag" property.
listView1.Items.Add(itmp).Tag = i;
You need to make sure that you do the same while adding items to the GridView too.
The DataGridView does not have an "Items" collection. To make it work, you need to bind the DataGridView to a collection of objects. Something like this should get you started:
List<Email> emails = new List<Email>();
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
emails.Add(email);
}
dataGridView.ItemsSource = emails;
You should not need to store the row index for each item in a "Tag" object - you can can get the selected index like this:
int selectedIndex = dataGridView.SelectedCells[0].RowIndex;