I’m sure there’s a simple solution to this…

I'm trying to make the "Current Count" column of my DataGridView only accept integers. I've had success when I use

 int value; string str = my_datagridview.Rows[1].Cells["Current Count"].Value.ToString(); if (int.TryParse(str, out value)) 

But I'm sure there must be a way where I don't have to write that code for every individual row. I've tried it with a for loop and it gives me a NullReferenceExeption.

Here's my code (including NullReferenceException for loop):

namespace WoodStock { public partial class Form1 : Form { //adds required elements DataGridView my_datagridview = new DataGridView(); DataTable my_datatable = new DataTable(); Button export_button = new Button(); //This textbox isn't visible to the user. It is only to temporarily store and allow for formatting //of the stocklist file. Code for testing purposes can be found below TextBox text_box_export = new TextBox(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Sets window sizes this.Size = new Size(600, 450); my_datagridview.Size = new Size(450, 400); my_datagridview.Location = new Point(5, 5); text_box_export.Multiline = true; export_button.Size = new Size(100, 50); export_button.Location = new Point(470, 5); export_button.Text = "Export data"; //makes datagrid & button visible this.Controls.Add(export_button); this.Controls.Add(my_datagridview); //To make text_box_export visible for testing purposes uncomment this code block: //text_box_export.Size = new Size(350, 340); //text_box_export.Location = new Point(470, 65); //this.Controls.Add(text_box_export); //binds data source to datagrid my_datagridview.DataSource = my_datatable; //button click event export_button.Click += new EventHandler(export_button_Click); //opens .csv file into string array string[] raw_text = System.IO.File.ReadAllLines("c:\\StockFile\\stocklist.csv"); //splits data into cells by looking for commas string[] data_col = null; int x = 0; foreach (string text_line in raw_text) { data_col = text_line.Split(','); if (x == 0) { //to determine headers for (int i = 0; i <= data_col.Count() - 1; i++) { my_datatable.Columns.Add(data_col[i]); } x++; } else { //to determine data my_datatable.Rows.Add(data_col); } } //Ensures only 'Current Count' column is editable my_datagridview.ReadOnly = false; my_datagridview.Columns[0].ReadOnly = true; my_datagridview.Columns[1].ReadOnly = true; my_datagridview.Columns[3].ReadOnly = true; } //'Export data' button void export_button_Click(object sender, EventArgs e) { for (int i = 0; i < my_datagridview.RowCount; i++) { int value; string str = my_datagridview.Rows[i].Cells["Current Count"].Value.ToString(); if (int.TryParse(str, out value)) { //clears form and ensures new file will have header text_box_export.Text = "Item Code,Item Description,Current Count,On Order\r\n"; //ints for for loops int row_count = my_datagridview.RowCount; int cell_count = my_datagridview.Rows[0].Cells.Count; //captures data for each row for (int row_index = 0; row_index <= row_count - 2; row_index++) { //captures data for each column in each row for (int cell_index = 0; cell_index < cell_count; cell_index++) { //adds data to messagebox in stocklist format & leaves comma off the end of each line if (cell_index <= 2) { text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString() + ","; } else { text_box_export.Text = text_box_export.Text + my_datagridview.Rows[row_index].Cells[cell_index].Value.ToString(); } } text_box_export.Text = text_box_export.Text + "\r\n"; } //writes new data from messagebox over stocklist.csv file System.IO.File.WriteAllText("c:\\Stockfile\\stocklist.csv", text_box_export.Text); } else { MessageBox.Show("Data not saved. Please enter only integers"); } } } } } 

by Mopstrr via /r/csharp

Leave a Reply