//ADDS NEW DATAGRIDVIEW ROW dgv.Rows.Add(txtFirstName.Text.ToString());
//ADDS THE SUBITEMS OF THE CURRENTLY ADDED ROW dgv.Rows[dgv.Rows.Count - 1].Cells[1].Value = txtLastName.Text.ToString(); dgv.Rows[dgv.Rows.Count - 1].Cells[2].Value = Convert.ToInt16(txtAge.Text);
//SELECT THE LAST ROW OF THE DATAGRIDVIEW dgv.Rows[dgv.Rows.Count - 1].Selected = true; } }
Directory listings are just HTML pages generated by a web server. Each web server generates these HTML pages in its own way because there is no standard way for a web server to list these directories.
The web server you'd like to list directories from must have directory browsing turned on to get this HTML representation of the files in its directories. So you can only get the directory listing if the HTTP server wants you to be able to.
[C#]
using System; using System.IO; using System.Net; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions;
namespace DirectoryListing { public partial class frmMainDownloader : Form { public frmMainDownloader() { InitializeComponent(); }
public static string GetDirectoryListingRegexForUrl(string url) { return "<a href=\".*\">(?<name>.*)</a>"; }
if (url.Equals("")) { MessageBox.Show("Please input the Webpage Url you want to parse.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Error); txtUrl.Focus(); return; }
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string html = reader.ReadToEnd();
Regex regex = new Regex(GetDirectoryListingRegexForUrl(url)); MatchCollection matches = regex.Matches(html);
pbar.Maximum = matches.Count; pbar.Step = 100;
if (matches.Count > 0) { foreach (Match match in matches) { if (match.Success) { fileDir = match.Groups["name"].ToString();
if (fileDir != "Parent Directory") //EXCLUDES THE PARENT DIRECTORY { list.Items.Add(fileDir); pbar.Value ++;
The following snippet allows you to check if a process is already running in the background. If the process is already running it will prompt the user to whether start a new process or continue with the existing one that is running.
private void checkForDataInProgress() { if (!this.bConnectedToDatabase) return;
The following example loads the data of the Selected Row in a DataGridView control into a TextBox control in CSharp. As shown on the previous example Populate Data Into DataGridView using For-Loop in C#/CSharp, we'll use that method to fill our DataGridView with data. To begin with: 1.) Start start a new C# project. 2.) Add the following controls onto Form1.
(1) DataGridView Control
(2) TextBoxes
(2) Labels
3.) Open the Windows Form Designer and Update the DataGridView's generated code with the following.
// // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Location = new System.Drawing.Point(12, 12); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(240, 221); this.dataGridView1.TabIndex = 0; this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
4.) Add the following codes into the Form1_Load() Event to fill the dataGridView control with data.
Random rand = new Random(); DataGridView dgv = this.dataGridView1;
The following codes will always Show the Selected Row of a DatagridView when the data is reloaded and will recall the state of the scrollbar by its scroll value..
private void datagridView1_Scroll(object sender, EventArgs e) { //WILL USE THE SCROLL VALUE LATER WHEN RELOADING THE DATAGRIDVIEW scrollVal = datagridView1.VerticalScrollingOffset; }
private void datagridView1_CellClick(object sender, DataGridViewCellEventArgs e) { //GETS THE INDEX OF THE CURRENT SELECTED ROW rowIndex = this.datagridView1.CurrentRow.Index; rowIndex = rowIndex != 0 ? lastRow = rowIndex : lastRow = rowIndex; }
private void reloadDatagridView() { //RELOADS DATA OF THE DATAGRIDVIEW datagridView1.Clear(); datagridView1.DataSource = "Your Data Table"; datagridView1.AutoGenerateColumns = true; datagridView1.MultiSelect = false; datagridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader); datagridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
datagridView1.Rows[lastRow].Selected = true;
int rowHeight = datagridView1.Rows.GetRowsHeight(DataGridViewElementStates.Selected);
HOW IT WORKS: scrollVal: Every time the user scroll's the control it will get the scrolling offset of the scroll bars.
lastRow: When the user clicks anywhere on the DatagridView's Cell it will get the Selected Row's Index and assign to lastRow variable. The lastRow variable will then be used as the Index of the Row that will be selected when the DatagridView is reloaded.
rowHeight: Get's the height of the DatagridViewRow's Selected element state.
.FirstDisplayedScrollingRowIndex = scrollVal / rowHeight: Set's the Scroll Value of the DatagridView when the data is reloaded.