Creating dynamic columns and data binding - c#

I am using ObjectListView to display information regarding virtual machines and each virtual machine has a different set of drives attached to it. During my data gathering, I get all of the info and store it in a Disk_Drive object with the properties that I care for (size,serial,usage,etc), now I want to dynamically create the columns and then bind the data using AddObjects. How would I do this?
I am not sure what the aspect getter should be in this scenario as it will not be unique, how can I handle this?
public void GenerateColumns(Model.HyperVTools.VMInfo vmObject)
{
objectListView2.Columns.Clear();
objectListView2.Items.Clear();
List<OLVColumn> columnsList = new List<OLVColumn>();
OLVColumn vmhostnameColumn = new OLVColumn("Hostname", "Host");
//vhd columns
foreach (var disk in vmObject.DisksList)
{
string text = string.Format("{0};{1}GB;{2}GB;", disk.Path, disk.SizeReadable,
disk.MaxVHDSizeReadable);
disk.FormattedVHDInfo = text;
OLVColumn diskColumn = new OLVColumn("Attached VHD", "FormattedVHDInfo");
columnsList.Add(diskColumn);
}
columnsList.Add(vmhostnameColumn);
objectListView2.Columns.AddRange(columnsList.Cast<System.Windows.Forms.ColumnHeader>().ToArray());
objectListView2.AddObject(vmObject);
}

Try this:
DataTable dtInput = new DataTable();
dtInput = dsAnalystPage.Tables[0];
foreach (DataColumn cl in dtInput.Columns)
{
BrightIdeasSoftware.OLVColumn aNewColumn = new BrightIdeasSoftware.OLVColumn();
aNewColumn.Name = cl.ColumnName;
aNewColumn.Text = cl.ColumnName;
if (aNewColumn.Text == "ASF_Code" || aNewColumn.Text == "ASD_SheetCode" || aNewColumn.Text == "ASD_SheetName")
{
aNewColumn.Width = 0;
aNewColumn.IsVisible = false;
}
OLV.AllColumns.Add(aNewColumn);
OLV.RebuildColumns();
}

Try resetting the control.
OLV.Reset();

Related

Checkbox in DataGridView doesnt Check

I create a DataGridView as follows
private void IniciarGV()
{
using (var db = new Entities())
{
var ListaPantallas = (from a in db.PANTALLAS
select a).ToList();
if (ListaPantallas != null)
{
gvPermisos.DataSource = ListaPantallas;
gvPermisos.Columns["idPantalla"].Visible = false;
gvPermisos.Columns["nombrepantalla"].HeaderText = "Nombre";
//gvPermisos.Columns.Add(new DataGridViewCheckBoxColumn());
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "Seleccione";
checkBoxColumn.Width = 50;
checkBoxColumn.Name = "checkBoxColumn";
gvPermisos.Columns.Insert(0, checkBoxColumn);
//gvPermisos.EndEdit();
}
db.Dispose();
}
}
After that a i retrieve the privileges with the same method linq
var TraerPermisos = (from a in db.PERMISOS
where a.IDUSUARIO == UsuarioEditar.idUsuario
select a).ToList();
After i go through the gridview to match the privileges with the ids that i wanted to check so the user can edit them, but for some reason they always appear unmarked
so far this is what i encounter in every forum or google, but it doesnt seem to work
foreach (PERMISOS item in TraerPermisos)
{
foreach (DataGridViewRow row in gvPermisos.Rows)
{
var ValorPermisoEnGV = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
var ValorPantalla = decimal.Parse(row.Cells["idPantalla"].Value.ToString());
if (ValorPantalla == item.IDPANTALLA)
{
row.Cells["checkBoxColumn"].Value = true;
}
}
}
this what my form looks like
I load the privileges on start
public ManejarUsuario()
{
InitializeComponent();
IniciarComboBox();
IniciarGV();
if (UsuarioEditar.idUsuario != 0)
{
CargarDatos();
btnCrearUsuario.Text = "Editar";
CargarPrivilegios();
}
}
Sorry about the long post, i've tried so many options but none seems to work
Life would be a lot easier if you just put a Boolean in the data driving the grid. First, let's make a holder class for your info
class Perms{
public int IdPantalla { get; set; }
public string NombrePantalla { get; set; }
public bool HasPermission { get; set; }
}
Then let's have the query produce a list of these instead:
//get all the IDPANTALLA this user has and put in a hash set
var TraerPermisos = (from a in db.PERMISOS
where a.IDUSUARIO == UsuarioEditar.idUsuario
select a.IDPANTALLA).ToHashSet();
//make a list of objects that includes a bool of whether the user has that permission
//note you should have this be a class level variable for ease of use later/elsewhere
ListaPantallas = (from a in db.PANTALLAS
select new Perms {
IdPantalla a.IdPantalla,
HasPermission = TraerPermisos.Contains(a.idPantalla) ,
NombrePantalla = a.nombrepantalla
}).ToList();
Then set up the grid:
gvPermisos.DataSource = ListaPantallas;
gvPermisos.Columns["IdPantalla"].Visible = false;
gvPermisos.Columns[ "NombrePantalla"].HeaderText = "Nombre";
gvPermisos.Columns[ "HadPermission"].HeaderText = "Seleccione";
The grid will find the bool property in the list and wire it up for you as a check column. It doesn't need you to make a column for it. Anything you tick is then stored in the underlying list as a true/false and you can eg foreach(var x in ListaPantallas) if(x.HasPermission...
When manipulating data in a DGV that is bound to a data source, manipulate the source. Also consider to make Perms implements INotifyPropertyChanged (and consider switching to using a binding list)

C# Reading CSV to DataTable and Invoke Rows/Columns

i am currently working on a small Project and i got stuck with a Problem i currently can not manage to solve...
I have multiple ".CSV" Files i want to read, they all have the same Data just with different Values.
Header1;Value1;Info1
Header2;Value2;Info2
Header3;Value3;Info3
While reading the first File i Need to Create the Headers. The Problem is they are not splited in Columns but in rows (as you can see above Header1-Header3).
Then it Needs to read the Value 1 - Value 3 (they are listed in the 2nd Column) and on top of that i Need to create another Header -> Header4 with the data of "Info2" which is always placed in Column 3 and Row 2 (the other values of Column 3 i can ignore).
So the Outcome after the first File should look like this:
Header1;Header2;Header3;Header4;
Value1;Value2;Value3;Info2;
And after multiple files it sohuld be like this:
Header1;Header2;Header3;Header4;
Value1;Value2;Value3;Value4;
Value1b;Value2b;Value3b;Value4b;
Value1c;Value2c;Value3c;Value4c;
I tried it with OleDB but i get the Error "missing ISAM" which i cant mange to fix. The Code i Used is the following:
public DataTable ReadCsv(string fileName)
{
DataTable dt = new DataTable("Data");
/* using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(fileName) + "\";Extendet Properties ='text;HDR=yes;FMT=Delimited(,)';"))
*/
using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Path.GetDirectoryName(fileName) + ";Extendet Properties ='text;HDR=yes;FMT=Delimited(,)';"))
{
using(OleDbCommand cmd = new OleDbCommand(string.Format("select *from [{0}]", new FileInfo(fileName).Name,cn)))
{
cn.Open();
using(OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))
{
adapter.Fill(dt);
}
}
}
return dt;
}
Another attempt i did was using StreamReader. But the Headers are in the wrong place and i dont know how to Change this + do this for every file. the Code i tried is the following:
public static DataTable ReadCsvFilee(string path)
{
DataTable oDataTable = new DataTable();
var fileNames = Directory.GetFiles(path);
foreach (var fileName in fileNames)
{
//initialising a StreamReader type variable and will pass the file location
StreamReader oStreamReader = new StreamReader(fileName);
// CONTROLS WHETHER WE SKIP A ROW OR NOT
int RowCount = 0;
// CONTROLS WHETHER WE CREATE COLUMNS OR NOT
bool hasColumns = false;
string[] ColumnNames = null;
string[] oStreamDataValues = null;
//using while loop read the stream data till end
while (!oStreamReader.EndOfStream)
{
String oStreamRowData = oStreamReader.ReadLine().Trim();
if (oStreamRowData.Length > 0)
{
oStreamDataValues = oStreamRowData.Split(';');
//Bcoz the first row contains column names, we will poluate
//the column name by
//reading the first row and RowCount-0 will be true only once
// CHANGE TO CHECK FOR COLUMNS CREATED
if (!hasColumns)
{
ColumnNames = oStreamRowData.Split(';');
//using foreach looping through all the column names
foreach (string csvcolumn in ColumnNames)
{
DataColumn oDataColumn = new DataColumn(csvcolumn.ToUpper(), typeof(string));
//setting the default value of empty.string to newly created column
oDataColumn.DefaultValue = string.Empty;
//adding the newly created column to the table
oDataTable.Columns.Add(oDataColumn);
}
// SET COLUMNS CREATED
hasColumns = true;
// SET RowCount TO 0 SO WE KNOW TO SKIP COLUMNS LINE
RowCount = 0;
}
else
{
// IF RowCount IS 0 THEN SKIP COLUMN LINE
if (RowCount++ == 0) continue;
//creates a new DataRow with the same schema as of the oDataTable
DataRow oDataRow = oDataTable.NewRow();
//using foreach looping through all the column names
for (int i = 0; i < ColumnNames.Length; i++)
{
oDataRow[ColumnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString();
}
//adding the newly created row with data to the oDataTable
oDataTable.Rows.Add(oDataRow);
}
}
}
//close the oStreamReader object
oStreamReader.Close();
//release all the resources used by the oStreamReader object
oStreamReader.Dispose();
}
return oDataTable;
}
I am thankful for everyone who is willing to help. And Thanks for reading this far!
Sincerely yours
If I understood you right, there is a strict parsing there like this:
string OpenAndParse(string filename, bool firstFile=false)
{
var lines = File.ReadAllLines(filename);
var parsed = lines.Select(l => l.Split(';')).ToArray();
var header = $"{parsed[0][0]};{parsed[1][0]};{parsed[2][0]};{parsed[1][0]}\n";
var data = $"{parsed[0][1]};{parsed[1][1]};{parsed[2][1]};{parsed[1][2]}\n";
return firstFile
? $"{header}{data}"
: $"{data}";
}
Where it would return - if first file:
Header1;Header2;Header3;Header2
Value1;Value2;Value3;Value4
if not first file:
Value1;Value2;Value3;Value4
If I am correct, rest is about running this against a list file of files and joining the results in an output file.
EDIT: Against a directory:
void ProcessFiles(string folderName, string outputFileName)
{
bool firstFile = true;
foreach (var f in Directory.GetFiles(folderName))
{
File.AppendAllText(outputFileName, OpenAndParse(f, firstFile));
firstFile = false;
}
}
Note: I missed you want a DataTable and not an output file. Then you could simply create a list and put the results into that list making the list the datasource for your datatable (then why would you use semicolons in there? Probably all you need is to simply attach the array values to a list).
(Adding as another answer just to make it uncluttered)
void ProcessMyFiles(string folderName)
{
List<MyData> d = new List<MyData>();
var files = Directory.GetFiles(folderName);
foreach (var file in files)
{
OpenAndParse(file, d);
}
string[] headers = GetHeaders(files[0]);
DataGridView dgv = new DataGridView {Dock=DockStyle.Fill};
dgv.DataSource = d;
dgv.ColumnAdded += (sender, e) => {e.Column.HeaderText = headers[e.Column.Index];};
Form f = new Form();
f.Controls.Add(dgv);
f.Show();
}
string[] GetHeaders(string filename)
{
var lines = File.ReadAllLines(filename);
var parsed = lines.Select(l => l.Split(';')).ToArray();
return new string[] { parsed[0][0], parsed[1][0], parsed[2][0], parsed[1][0] };
}
void OpenAndParse(string filename, List<MyData> d)
{
var lines = File.ReadAllLines(filename);
var parsed = lines.Select(l => l.Split(';')).ToArray();
var data = new MyData
{
Col1 = parsed[0][1],
Col2 = parsed[1][1],
Col3 = parsed[2][1],
Col4 = parsed[1][2]
};
d.Add(data);
}
public class MyData
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
public string Col4 { get; set; }
}
I don't know if this is the best way to do this. But what i would have done in your case, is to rewrite the CSV's the conventionnal way while reading all the files, then create a stream containing the new CSV created.
It would look like something like this :
var csv = new StringBuilder();
csv.AppendLine("Header1;Header2;Header3;Header4");
foreach (var item in file)
{
var newLine = string.Format("{0},{1},{2},{3}", item.value1, item.value2, item.value3, item.value4);
csv.AppendLine(newLine);
}
//Create Stream
MemoryStream stream = new MemoryStream();
StreamReader reader = new StreamReader(stream);
//Fill your data table here with your values
Hope this will help.

How to bind listview while ajaxcalling in webmethod

[WebMethod]
public static string DeletePrescription(int PrescriptionId)
{
BusinessLogicLayer objBusiness = new BusinessLogicLayer();
SanatanJeevanBusinessObjects.Prescription objPrescription = new SanatanJeevanBusinessObjects.Prescription();
objPrescription.PrescriptionId = Convert.ToInt32(PrescriptionId);
objBusiness.DeletePrescriptionBAL(objPrescription);
DataSet ds = new DataSet();
ds = objBusiness.GetPrescription(objPrescription);
listPreDetails.DataSource = ds;
listPreDetails.DataBind();
return "success";
}
hi,
i am using ajax function to delete a row after delete the row in the table bind the data in listview. please help me.
Although youll be deleting the item in the code behind it will still stay on the page because theres no way of interacting with your controls on the page
I have similar requirements but then remove the parent row from the screen using the JQuery below.
function Delete(idToDelete) {
var par = $(this).parent().parent(); //tr
var id;
if (idToDelete.selector == undefined) {
if (!$(this.id).selector == "") {
id = $(this.id).selector;
} else {
id = idToDelete;
}
} else {
id = idToDelete;
}
par.remove();
};
This works for me every time now.

What does the data source need to have for a GridView to autogenerate columns?

I created custom datatype classes that format the data the way I need it. The data I retrieve from the database comes as .NET base types, so I need to loop through the DataTable, convert each item into it's custom type, and put the converted item into a new table. I also copy the column names from the old table to the new one. The problem is, when I bind a GridView to my new table, it throws an exception:
HttpException:
The data source for GridView with id 'TestGrid' did not have any properties
or attributes from which to generate columns. Ensure that your data source
has content.
Stack Trace:
at System.Web.UI.WebControls.GridView.CreateAutoGeneratedColumns(PagedDataSource dataSource)
at System.Web.UI.WebControls.GridView.CreateColumns(PagedDataSource dataSource, Boolean useDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
What do I need to add to my DataTable to get autogeneratecolumns to work?
Edit: Here is the code for the DataTable:
// Populate first DataTable with data from database.
adapter = new DataAdapter("SELECT status, date_ordered, date_due FROM import_table", OpenConnection);
DataTable originalTable = new DataTable();
adapter.Fill(originalTable)
// Second DataTable for converted table data.
DataTable convertedTable = new DataTable();
// The list of custom datatypes to convert to.
Type[] newTypes = {typeof(TrackingStatus), typeof(Date), typeof(Date)};
// Set the ColumnName and DataType on each column of the new table.
for(int i = 0; i < originalTable.Columns.Count; i++)
{
convertedTable.Columns.Add();
convertedTable.Columns[i].ColumnName = originalTable.Columns[i].ColumnName;
if(newTypes.Length > i)
convertedTable.Columns[i].DataType = newTypes[i];
}
// Convert each item from the old table and add it to the new table.
foreach(DataRow oldRow in originalTable.Rows)
{
DataRow newRow = convertedTable.NewRow();
for(int i = 0; i < convertedTable.Columns.Count; i++)
{
if(newTypes.Length <= i)
newRow[i] = oldRow[i];
else if(newTypes[i] == typeof(Date))
newRow[i] = Date.FromObject(oldRow[i]);
else if(newTypes[i] == typeof(TrackingStatus))
newRow[i] = TrackingStatus.FromObject(oldRow[i]);
else if(newTypes[i] == typeof(EmailAddress))
newRow[i] = EmailAddress.FromObject(oldRow[i]);
}
convertedTable.Rows.Add(newRow);
}
// Bind the GridView.
displayGrid.DataSource = convertedTable;
displayGrid.DataBind();
Instead of creating a new datatable when you convert each to custom types, create a new List and bind the list to the datasource. e.g.
var list = new List<MyClass>();
var myType = //Convert your type
list.Add(myType);
grid.DataSource = list;
And ofcourse this all is just an idea of how to do it.
UPDATE:
Saw your code afterwards:
Try this:
Create a class called ImportRow
class ImportRow
{
private string m_Status = string.Empty;
private DateTime m_DateOrdered;
private DateTime m_DateDue;
public ImportRow() { }
public string Status
{
get { return m_Status; }
set { m_Status = value; }
}
public DateTime DateOrdered
{
get { return m_DateOrdered; }
set { m_DateOrdered = value; }
}
public DateTime DateDue
{
get { return m_DateDue; }
set { m_DateDue = value; }
}
}
Then use it as:
var importedData = new List<ImportRow>();
foreach (DataRow oldRow in originalTable.Rows)
{
var newRow = new ImportRow();
newRow.Status = oldRow["status"].ToString();
newRow.DateDue = Convert.ToDateTime(oldRow["date_due"].ToString());
newRow.DateOrdered = Convert.ToDateTime(oldRow["date_ordered"].ToString());
importedData.Add(newRow);
}
Then
displayGrid.DataSource = importedData;
displayGrid.DataBind();
Your custom type needs public properties like these:
public class Foo
{
public int Id;
public string Name;
}
or you need to use the DataTable itself as DataSource.

Get value from column using a column name string c#

I have 2 lists..
The first contains rows with mapping values inlcuding column name, xcord, ycord
The second contains the data I need to map..
I need to get the value in each row using the column name from the first row..
for example
List<SheetMappings> smaps = new List<SheetMappings>();
foreach(maplist m in mlist)
{
SheetMappings newMap = new SheetMappings();
foreach(vallist v in vlist)
{
newMap.Value = v.{m.ColumnName};
newMap.xCord = m.xCord;
newMap.yCord = m.yCord;
}
smaps.Add(newMap);
}
Any assitance appreciated
Cheers
Graham
EDIT:
List<SpreadMappings> spreadMapping = new List<SpreadMappings>();
foreach (var m in mappings)
{
foreach (var v in hvalues)
{
SpreadMappings map = new SpreadMappings();
switch (m.ColumnName)
{
case “DocHeading”:
map.ColumnX = m.ColumnX;
map.ColumnY = m.ColumnY;
map.ColumnValue = v.DocHeading;
map.ColumnName = m.ColumnName;
map.ColumnId = v.Id;
map.ColumnSheetName = sheetName; spreadMapping.Add(map);
break;
If I understand what you're trying to do, you'll need to use reflection to get the value of the property represented by m.ColumnName:
var smaps = new List<SheetMappings>();
foreach(maplist m in mlist)
{
var pi = typeof(vallist).GetProperty(m.ColumnName);
var newMap = new SheetMappings();
foreach(vallist v in vlist)
{
newMap.Value = pi.GetValue(v, null);
newMap.xCord = m.xCord;
newMap.yCord = m.yCord;
}
smaps.Add(newMap);
}
So that's using reflection to get a reference to the PropertyInfo for the property represented by m.ColumnName, then calling PropertyInfo.GetValue to get the value of that property from v.
Well I think the "newMap.Value = v.{m.ColumnName}" part would be something like:
newMap.Value = v.FirstOrDefault( vitem => vitem.ColumnName == m.ColumnName );
This would give you the first item within "v" that has a "ColumnName" property that matches the "ColumnName" property of "m". This assumes that the contents of "vallist" are objects that have a "ColumnName" property.

Categories

Resources