T
Tom
I have populated a List with objects containing FileInfo. This List is
bound to a DataGridView and the contents *are* visible within the
DataGridView control. When I click within the DataGridView I get the
following error >>
Index -1 does not have a value.
Perhaps I simply need to understand how to set the index correctly? Or
maybe I need to assign values to the row & column headers? Or any of a
host of typical *newbie* mistakes.
Please point me in the right direction.
Thanks !!
-- Tom
// Primary Statements Involving DataGridView & Data Classes >>
public class FilePicker2 : Form
{
DataGridView grid;
BindingSource bindsrc = new BindingSource();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new FilePicker2());
}
public FilePicker2()
{
bindsrc.DataSource = new Directory();
bindsrc.DataMember = "FilesInfo";
grid = new DataGridView();
grid.Parent = split1.Panel2;
grid.AutoSize = true;
grid.Dock = DockStyle.Fill;
grid.AutoGenerateColumns = false; // Columns Manually Added Below
grid.DataSource = bindsrc;
DataGridViewTextBoxColumn colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Name";
colText.HeaderText = "Name";
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Ext";
colText.HeaderText = "Ext.";
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Size";
colText.HeaderText = "Size";
colText.DefaultCellStyle.Alignment =
DataGridViewContenetAlignment.MiddleRight;
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "ModifyTime";
colText.HeaderText = "Date & Time";
grid.Columns.Add(colText);
grid.BorderStyle = BorderStyle.None;
grid.CellBorderStyle = DataGridViewCellBorderStyle.None;
grid.RowHeadersVisible = false;
grid.ScrollBars = ScrollBars.Vertical; // << Not Working
grid.AllowUserToOrderColumns = true;
grid.ReadOnly = true;
grid.AllowUserToAddRows = false;
grid.AllowUserToAddColumns = false;
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
} // End of Constructor
// The following is called upon directory selection in a TreeView
public void Fill_dirList(string path)
{
DirectoryInfo dirinfo = new DirectoryInfo(path);
Directory.filelist.Clear();
foreach (FileInfo file in dirinfo.GetFiles())
{
FileDetail temp = new FileDetail(file);
Directory.filelist.Add(temp); // << Filling the List
// That Is Bound to
// the DataGridView
}
}
} // public class FilePicker2 : Form
// Partial Class Information >>
public class Directory
{
public static List<FileDetail> filelist = new List<FileDetail>();
public List<FileDetail> FilesInfo
{
set { filelist = value; } // << No Change When Commented Out
get ( return filelist: }
}
}
// ###############################################
public class FileDetail
{
private string name, ext, size, modifytime;
public FileDetail(FileInfo fileInfo)
{
this.name = fileInfo.Name;
this.ext = fileInfo.Extension:
this.size = fileInfo.Length.ToString());
this.modifyTime =
fileInfo.LastWriteTime.ToString("yyy'-'MM'-'dd' HH':'mm");
}
public string Name { get { return this.name; } }
public string Ext { get { return this.ext; } }
public string Size { get { return this.size; } }
public string ModifyTime { get { return this.modifyTime; } }
}
bound to a DataGridView and the contents *are* visible within the
DataGridView control. When I click within the DataGridView I get the
following error >>
Index -1 does not have a value.
Perhaps I simply need to understand how to set the index correctly? Or
maybe I need to assign values to the row & column headers? Or any of a
host of typical *newbie* mistakes.
Please point me in the right direction.
Thanks !!
-- Tom
// Primary Statements Involving DataGridView & Data Classes >>
public class FilePicker2 : Form
{
DataGridView grid;
BindingSource bindsrc = new BindingSource();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new FilePicker2());
}
public FilePicker2()
{
bindsrc.DataSource = new Directory();
bindsrc.DataMember = "FilesInfo";
grid = new DataGridView();
grid.Parent = split1.Panel2;
grid.AutoSize = true;
grid.Dock = DockStyle.Fill;
grid.AutoGenerateColumns = false; // Columns Manually Added Below
grid.DataSource = bindsrc;
DataGridViewTextBoxColumn colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Name";
colText.HeaderText = "Name";
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Ext";
colText.HeaderText = "Ext.";
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "Size";
colText.HeaderText = "Size";
colText.DefaultCellStyle.Alignment =
DataGridViewContenetAlignment.MiddleRight;
grid.Columns.Add(colText);
colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "ModifyTime";
colText.HeaderText = "Date & Time";
grid.Columns.Add(colText);
grid.BorderStyle = BorderStyle.None;
grid.CellBorderStyle = DataGridViewCellBorderStyle.None;
grid.RowHeadersVisible = false;
grid.ScrollBars = ScrollBars.Vertical; // << Not Working
grid.AllowUserToOrderColumns = true;
grid.ReadOnly = true;
grid.AllowUserToAddRows = false;
grid.AllowUserToAddColumns = false;
grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
} // End of Constructor
// The following is called upon directory selection in a TreeView
public void Fill_dirList(string path)
{
DirectoryInfo dirinfo = new DirectoryInfo(path);
Directory.filelist.Clear();
foreach (FileInfo file in dirinfo.GetFiles())
{
FileDetail temp = new FileDetail(file);
Directory.filelist.Add(temp); // << Filling the List
// That Is Bound to
// the DataGridView
}
}
} // public class FilePicker2 : Form
// Partial Class Information >>
public class Directory
{
public static List<FileDetail> filelist = new List<FileDetail>();
public List<FileDetail> FilesInfo
{
set { filelist = value; } // << No Change When Commented Out
get ( return filelist: }
}
}
// ###############################################
public class FileDetail
{
private string name, ext, size, modifytime;
public FileDetail(FileInfo fileInfo)
{
this.name = fileInfo.Name;
this.ext = fileInfo.Extension:
this.size = fileInfo.Length.ToString());
this.modifyTime =
fileInfo.LastWriteTime.ToString("yyy'-'MM'-'dd' HH':'mm");
}
public string Name { get { return this.name; } }
public string Ext { get { return this.ext; } }
public string Size { get { return this.size; } }
public string ModifyTime { get { return this.modifyTime; } }
}