Iterate through specific col in DGVs' & compare the string values - c#

I got 2 datagrid views, First datagrid 1st col or Index 0 & the Second datagrid 1st col or Index 0.
How can i loop through a specific col in the datagridviews and look for the string values,if it matches with the list then go to a function.
My approach is not working. How can i do this?
private void b_calculate_Click(object sender, EventArgs e)
{
List<string> value = new List<String>() { "AE0", "AT1", "AT2", "AT3"};
value = new List<string>(datagridview1.Columns[0].Index);
List<string> value2 = new List<String>() { "BE0", "BT1", "BT2", "BT3"};
value2 = new List<string>(datagridview2.Columns[0].Index);
//First Combination
if((value.ToString() == "AT1" || value.ToString() == "AE0" ||
value.ToString() == "AT2")
&&
(value2.ToString() == "BT1" || value2.ToString() == "BE0"))
{
gottoFunction1();
}
//Second Combination
if((value.ToString() == "AT1" || value.ToString() == "AT2" )
&&
(value2.ToString() == "BT1" || value2.ToString() == "BT2"))
{
gottoFunction2();
}
}

Here's a routine that iterates through all available rows in both DataGridViews and does the processing shown in your method:
private void b_calculate_Click(object sender, EventArgs e)
{
for (var i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView2.RowCount <= i)
break;
var cellFromDG1 = dataGridView1.Rows[i].Cells[0];
var cellFromDG2 = dataGridView1.Rows[i].Cells[0];
if (cellFromDG1.Value == null || cellFromDG2.Value == null)
{
// this could be the empty row that allows you to
// enter a new record
continue;
}
var value = cellFromDG1.Value.ToString();
var value2 = cellFromDG2.Value.ToString();
if ((value == "AT1" || value == "AE0" || value == "AT2") &&
(value2 == "BT1" || value2 == "BE0"))
{
gottoFunction1();
}
if ((value == "AT1" || value == "AT2") &&
(value2 == "BT1" || value2 == "BT2"))
{
gottoFunction2();
}
}
}

Related

BlackJack program freezes after starting the game

I'm creating a BlackJack game in C#, everything (I'm pretty sure) works, before it gets to my Do-While loop(where everything in the actual game happens.) and when it does, it just either freezes or nothing happens at all. I noticed when I get rid of my Do-While loop the game doesn't freeze, so I'm guessing it's in an infinite loop or something, but I'm not sure what to do here.
namespace Blackjack
{
public partial class Form1 : Form
{
public Boolean standClicked = false;
public Boolean hitClicked = false;
public Boolean over;
public Cards[] cardSet = new Cards[5];
public PictureBox[] picArray = new PictureBox[5];
public int points;
public int i;
public Form1()
{
InitializeComponent();
//Makes all card images hidden
Card1.Hide();
Card2.Hide();
Card3.Hide();
Card4.Hide();
Card5.Hide();
//Makes the win-lose label blank
winOrLoseLabel.Text = " ";
buttonHit.Hide();
buttonStand.Hide();
}
private void buttonExit_Click(object sender, EventArgs e)
{
//Closes the application
this.Close();
}
private Image SetImage(int cardValue)
{
Image _ = null;
if (cardValue >= 0)
{
for (int check = 0; check >= 50; check++)
{
if (cardValue == check)
{
_ = imageList1.Images[check];
}
}
}
if (_ == null)
{
_ = imageList1.Images[0];
}
return _;
}
public void buttonHit_Click(object sender, EventArgs e)
{
hitClicked = true;
if (hitClicked == true)
{
picArray[i].Image = SetImage(cardSet[i].GetValue());
points += cardSet[i].CardValue();
pointCounter.Text = points.ToString();
}
points += Convert.ToInt32(pointCounter.Text);
if (points >= 21)
over = true;
else
over = false;
i++;
}
private void buttonStand_Click(object sender, EventArgs e)
{
standClicked = true;
buttonHit.Enabled = false;
}
private void buttonPlay_Click_2(object sender, EventArgs e)
{
// Reveals all cards on the form
Card1.Show();
Card2.Show();
Card3.Show();
Card4.Show();
Card5.Show();
//Hides the logo as well as the play and exit buttons
pictureBox3.Hide();
buttonPlay.Hide();
buttonExit.Hide();
//Sets both hit and stand buttons to be enabled again
buttonHit.Show();
buttonStand.Show();
buttonHit.Enabled = true;
buttonStand.Enabled = true;
//Makes the win-lose label blank
winOrLoseLabel.Text = " ";
//Sets the "standClicked boolean value to false - to not automatically end the game
pointCounter.Text = "0";
points = Convert.ToInt32(pointCounter.Text);
//Makes the cardset values randomized
cardSet[0] = new Cards(0);
cardSet[0].Value = 0;
cardSet[0].SetCards();
cardSet[1] = new Cards(0);
cardSet[1].Value = 0;
cardSet[1].SetCards();
cardSet[2] = new Cards(0);
cardSet[2].Value = 0;
cardSet[2].SetCards();
cardSet[3] = new Cards(0);
cardSet[3].Value = 0;
cardSet[3].SetCards();
cardSet[4] = new Cards(0);
cardSet[4].Value = 0;
cardSet[4].SetCards();
// PictureBox array to allow changes to different PictureBoxes
PictureBox[] picArray = new PictureBox[5];
// Card thing
for (int x = 0; x > picArray.Length; x++)
{
picArray[x] = new PictureBox();
}
picArray[0] = Card1;
picArray[1] = Card2;
picArray[2] = Card3;
picArray[3] = Card4;
picArray[4] = Card5;
// Will automatically show you your first card, while the rest will stay hidden until you press the Hit button.
picArray[0].Image = SetImage(cardSet[0].GetValue());
points += cardSet[0].CardValue();
pointCounter.Text = points.ToString();
i = 1;
//Sets over boolean to false - to not automatically end the game
over = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox7_Click(object sender, EventArgs e)
{
}
private void Card2_Click(object sender, EventArgs e)
{
}
private void pictureBox3_Click(object sender, EventArgs e)
{
}
private void buttonPlay_Click(object sender, EventArgs e)
{
}
private void buttonPlay_Click_1(object sender, EventArgs e)
{
}
}
}
And my Card Class if you need it as well
using System;
public class Cards
{
Random rand = new Random();
public int Value = 0;
public Cards(int value)
{
this.Value = value;
}
public int GetValue()
{
return Value;
}
public int CardValue()
{
if (Value == 0 || Value == 1 || Value == 2 || Value == 3)
return 2;
else if (Value == 4 || Value == 5 || Value == 6 || Value == 7)
return 3;
else if (Value == 8 || Value == 9 || Value == 10 || Value == 11)
return 4;
else if (Value == 12 || Value == 13 || Value == 14 || Value == 15)
return 5;
else if (Value == 16 || Value == 17 || Value == 18 || Value == 19)
return 6;
else if (Value == 20 || Value == 21 || Value == 22 || Value == 23)
return 7;
else if (Value == 24 || Value == 25 || Value == 26 || Value == 27)
return 8;
else if (Value == 28 || Value == 29 || Value == 30 || Value == 31)
return 9;
else if (Value == 32 || Value == 33 || Value == 34 || Value == 35)
return 10;
else if (Value == 36 || Value == 37 || Value == 38 || Value == 39)
return 1;
else if (Value == 40 || Value == 41 || Value == 42 || Value == 43 || Value == 44 || Value == 45 || Value == 46 || Value == 47)
return 10;
else
return 2;
}
public void SetCards()
{
//Adds a random value to card
int ranumber = rand.Next(0, 47);
Value = ranumber;
}
}
I'm still sort of new at programming, and this is my first real project. Any help on how I can make my code better and or fix the problem would mean a million to me.

Transfering text from one form to another form with dataGridView in c#?

I am trying to make an If statement with two dataGridViews. in my application I have a button on my second form and two button cell dataGridViews on my first form. the application should run like this: the user clicks on a cell from the table and a text from a comboBox in form2 will be transferred to the clicked cell. for some reason I'm getting an error on my if statement in form2.
This is the error that I get:
An unhandled exception of type 'System.NullReferenceException' occurred in Top Shine.exe
Additional information: Object reference not set to an instance of an object.
This is my code for form1:
namespace Top_Shine
{
public partial class Top_Shine_Form : Form
{
public Top_Shine_Form()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView1.CurrentCell.Value == null)
{
var form2 = new Form2();
form2.tsf1 = this;
form2.ShowDialog();
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView1.CurrentCell != null
&& dataGridView1.CurrentCell.Value != null
&& dataGridView1.CurrentCell.Value.ToString() != "*")
{
var form3 = new Form3();
form3.f3tsf1 = this;
form3.ShowDialog();
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
|| dataGridView1.CurrentCell.Value.ToString() == "*")
{
var form2 = new Form2();
form2.tsf1 = this;
form2.ShowDialog();
}
//////////////////////////////////////////// Color Cells \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView1.CurrentCell != null
&& dataGridView1.CurrentCell.Value != null
&& dataGridView1.CurrentCell.Value.ToString() != "*"
&& dataGridView1.CurrentCell.Value.ToString().Contains(" X"))
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.GreenYellow;
dataGridView1.CurrentCell.Style = CellStyle;
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView1.CurrentCell != null
&& dataGridView1.CurrentCell.Value != null
&& dataGridView1.CurrentCell.Value.ToString() != "*")
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.Yellow;
dataGridView1.CurrentCell.Style = CellStyle;
}
dataGridView1.ClearSelection();
}
..................................................................................................................................................................
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView2.CurrentCell.Value == null)
{
var form2 = new Form2();
form2.tsf2 = this;
form2.ShowDialog();
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView2.CurrentCell != null
&& dataGridView2.CurrentCell.Value != null
&& dataGridView2.CurrentCell.Value.ToString() != "*")
{
var form3 = new Form3();
form3.f3tsf2 = this;
form3.ShowDialog();
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
|| dataGridView2.CurrentCell.Value.ToString() == "*")
{
var form2 = new Form2();
form2.tsf2 = this;
form2.ShowDialog();
}
//////////////////////////////////////////// Color Cells \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView2.CurrentCell != null
&& dataGridView2.CurrentCell.Value != null
&& dataGridView2.CurrentCell.Value.ToString() != "*"
&& dataGridView2.CurrentCell.Value.ToString().Contains(" X"))
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.GreenYellow;
dataGridView2.CurrentCell.Style = CellStyle;
}
else if (e.ColumnIndex >= 2
&& e.ColumnIndex != 14
&& e.RowIndex != -1
&& dataGridView2.CurrentCell != null
&& dataGridView2.CurrentCell.Value != null
&& dataGridView2.CurrentCell.Value.ToString() != "*")
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.Yellow;
dataGridView2.CurrentCell.Style = CellStyle;
}
dataGridView1.ClearSelection();
}
And this is the code for Form2:
private void button1_Click(object sender, EventArgs e)
{
if (tsf1.dataGridView1.CurrentCell.Selected)
{
if (interiorComboBox.SelectedItem != null)
{
tsf1.dataGridView1.CurrentCell.Value = interiorComboBox.Text;
}
if (ExteriorComboBox.SelectedItem != null)
{
tsf1.dataGridView1.CurrentCell.Value = ExteriorComboBox.Text;
}
if (fitrmvComboBox.SelectedItem != null)
{
tsf1.dataGridView1.CurrentCell.Value = fitrmvComboBox.Text;
}
if (washComboBox.SelectedItem != null)
{
tsf1.dataGridView1.CurrentCell.Value = washComboBox.Text;
}
this.Close();
}
else if (tsf2.dataGridView2.CurrentCell.Selected)
{
if (interiorComboBox.SelectedItem != null)
{
tsf2.dataGridView2.CurrentCell.Value = interiorComboBox.Text;
}
if (ExteriorComboBox.SelectedItem != null)
{
tsf2.dataGridView2.CurrentCell.Value = ExteriorComboBox.Text;
}
if (fitrmvComboBox.SelectedItem != null)
{
tsf2.dataGridView2.CurrentCell.Value = fitrmvComboBox.Text;
}
if (washComboBox.SelectedItem != null)
{
tsf2.dataGridView2.CurrentCell.Value = washComboBox.Text;
}
}
else if(interiorComboBox.SelectedItem == null
&& ExteriorComboBox.SelectedItem == null
&& fitrmvComboBox.SelectedItem == null
&& washComboBox.SelectedItem == null)
{
MessageBox.Show("Please Select a Worker");
}
}
What is happening and how do I fix it exactly?
Thanks for the help.

Checking if something is empty

I am currently checking if comboboxes and numericupdowns are empty but i am doing it with If statement.
Can you tell me if there is any other faster way of doing it?
Here is current code
private void button1_Click(object sender, EventArgs e)
{
if(comboBox1.SelectedItem != null)
{
if(comboBox2.SelectedItem != null)
{
if(numericUpDown1.Value != 0)
{
if(numericUpDown2.Value != 0)
{
if(numericUpDown3.Value != 0)
{
if(numericUpDown4.Value != 0)
{
string domacin = "" + comboBox1.GetItemText(comboBox1.SelectedItem);
int D_kosevaPrvoPoluvreme = (int)numericUpDown1.Value;
int D_kosevaDrugoPoluvreme = (int)numericUpDown3.Value;
int D_ukupnoKoseva = D_kosevaDrugoPoluvreme + D_kosevaPrvoPoluvreme;
int D_primljenihKosevaPrvoPoluvreme = (int)numericUpDown2.Value;
int D_primljenihKosevaDrugoPoluvreme = (int)numericUpDown4.Value;
int D_ukupnoPrimljenihKoseva = D_primljenihKosevaDrugoPoluvreme + D_primljenihKosevaPrvoPoluvreme;
string gost = "" + comboBox2.GetItemText(comboBox2.SelectedItem);
int G_kosevaPrvoPoluvreme = (int)numericUpDown2.Value;
int G_kosevaDrugoPoluvreme = (int)numericUpDown4.Value;
int G_ukupnoKoseva = G_kosevaDrugoPoluvreme + G_kosevaPrvoPoluvreme;
int G_primljenihKosevaPrvoPoluvreme = (int)numericUpDown1.Value;
int G_primljenihKosevaDrugoPoluvreme = (int)numericUpDown3.Value;
int G_ukupnoPrimljenihKoseva = G_primljenihKosevaDrugoPoluvreme + G_primljenihKosevaPrvoPoluvreme;
int rezultat;
Functions.odrediPobednika(out rezultat, D_ukupnoKoseva, G_ukupnoKoseva);
//SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\arist\Documents\VisualStudio2015\Projects\NBA\NBA\NBA.mdf;Integrated Security=True");
//SqlCommand cmd = new SqlCommand("", con);
}
}
}
}
}
}
}
I would use following fail-fast-validation approach with meaningful messages:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Meaningful message, user should know what he has done wrong", "Invalid xyz");
return;
}
else if (comboBox2.SelectedItem == null)
{
MessageBox.Show("Meaningful message, user should know what he has done wrong", "Invalid xyz");
return;
}
else if (numericUpDown1.Value == 0)
{
MessageBox.Show("Meaningful message, user should know what he has done wrong", "Invalid xyz");
return;
}
else if (numericUpDown2.Value == 0)
{
MessageBox.Show("Meaningful message, user should know what he has done wrong", "Invalid xyz");
return;
}
else if (numericUpDown3.Value == 0)
{
MessageBox.Show("Meaningful message, user should know what he has done wrong", "Invalid xyz");
return;
}
// insert code
}
It's neither shorter nor more efficient, but it's easy to read, to debug and most important: the user knows what went wrong. You should also use meaningful control-names.
Of course you could also use one if and concat all conditions with &&, but i'd prefer my first approach for the reasons mentioned above.
bool valid = comboBox1.SelectedItem != null
&& comboBox2.SelectedItem != null
&& numericUpDown1.Value != 0
&& numericUpDown2.Value != 0
&& numericUpDown3.Value != 0
&& numericUpDown4.Value != 0;
if(valid){
//Code
}
The first thing that springs to mind is to use one if statement
if(comboBox1.SelectedItem != null && comboBox2.SelectedItem != null && numericUpDown1.Value != 0 && numericUpDown2.Value != 0 && numericUpDown3.Value != 0 && numericUpDown4.Value != 0)
{
//Code
}
This can also be put onto multiple lines for readability
if(comboBox1.SelectedItem != null &&
comboBox2.SelectedItem != null &&
numericUpDown1.Value != 0 &&
numericUpDown2.Value != 0 &&
numericUpDown3.Value != 0 &&
numericUpDown4.Value != 0)
{
//Code
}
There's not much to optimize there in reference to execution times etc. But the code could be made a bit neater, a start could be something like:
private void button1_Click(object sender, EventArgs e)
{
if(comboBox1.SelectedItem != null &&
comboBox2.SelectedItem != null &&
numericUpDown1.Value != 0 &&
numericUpDown2.Value != 0 &&
numericUpDown3.Value != 0 &&
numericUpDown4.Value != 0)
{
string domacin = comboBox1.GetItemText(comboBox1.SelectedItem);
int D_kosevaPrvoPoluvreme = (int)numericUpDown1.Value;
int D_kosevaDrugoPoluvreme = (int)numericUpDown3.Value;
int D_ukupnoKoseva = D_kosevaDrugoPoluvreme + D_kosevaPrvoPoluvreme;
int D_primljenihKosevaPrvoPoluvreme = (int)numericUpDown2.Value;
int D_primljenihKosevaDrugoPoluvreme = (int)numericUpDown4.Value;
int D_ukupnoPrimljenihKoseva = D_primljenihKosevaDrugoPoluvreme + D_primljenihKosevaPrvoPoluvreme;
string gost = comboBox2.GetItemText(comboBox2.SelectedItem);
int G_kosevaPrvoPoluvreme = (int)numericUpDown2.Value;
int G_kosevaDrugoPoluvreme = (int)numericUpDown4.Value;
int G_ukupnoKoseva = G_kosevaDrugoPoluvreme + G_kosevaPrvoPoluvreme;
int G_primljenihKosevaPrvoPoluvreme = (int)numericUpDown1.Value;
int G_primljenihKosevaDrugoPoluvreme = (int)numericUpDown3.Value;
int G_ukupnoPrimljenihKoseva = G_primljenihKosevaDrugoPoluvreme + G_primljenihKosevaPrvoPoluvreme;
int rezultat;
Functions.odrediPobednika(out rezultat, D_ukupnoKoseva, G_ukupnoKoseva);
//SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\arist\Documents\VisualStudio2015\Projects\NBA\NBA\NBA.mdf;Integrated Security=True");
//SqlCommand cmd = new SqlCommand("", con);
}
}

Windows phone app t9 predictive text

I am working on a windows phone app, which is a dialer and I have few problems with the predictive text. Predictive text works fine but it's laggy and slow. My code is:
I've put a contact search function in the textbox's text changed event:
private void dialer_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
MainPage.DialerText = dialer.Text;
contactSearch(MainPage.DialerText);
}
catch (Exception f)
{
MessageBox.Show(f.Message);
}
}
ContactSearch function code:
public void contactSearch(string str)
{
try
{
var digitMap = new Dictionary<int, string>() {
{ 1, "" },
{ 2, "[abcABC]" },
{ 3, "[defDEF]" },
{ 4, "[ghiGHI]" },
{ 5, "[jklJKL]" },
{ 6, "[mnoMNO]" },
{ 7, "[pqrsPQRS]" },
{ 8, "[tuvTUV]" },
{ 9, "[wxyzWXYZ]" },
{ 0, "" },
};
var enteredDigits = str;
var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
var regexBuilder = new StringBuilder();
foreach (var val in charsAsInts)
regexBuilder.Append(digitMap[val]);
var pattern = regexBuilder.ToString();
//append a ".*" to the end of the regex to make it "StartsWith", beginning for "EndsWith", or both for "Contains";
pattern = ".*" + pattern + ".*";
SearchListbox.ItemsSource = listobj.FindAll(x => x.PhoneNumbers.Contains(str) | Regex.IsMatch(x.FirstName, pattern));
}
catch (Exception e)
{
// MessageBox.Show(e.Message);
}
}
This code works fine but laggy and slow. I need to make it faster. Please suggest some improvements. Thank you.
These solutions can improve it:
1- Don't use Try catch for repeatedly functions:
This can slow down the performance easily, instead try using if else
2 - Use an asynchronous Task:
It's great for not blocking UI and avoids lag and crashes but use a low delay time for your task e.g 50ms;
3- Use other collection types:
As i can see in your code , your dictionary key is int started regularly from 1.
So why don't you use a array or list which are much faster with more items.
Like this:
string[] contacts = {"contact 1","contact 2","contact 3","contact 4"};
Or even a list:
List<string> contacts = new List<string>();
contacts.Add("contact 1");
contact.Add("contact 2");
Note that Changing collection type may increase performance only in huge amounts of data.
public interface ICellT9
{
void Add(string a_name);
List<string> GetNames(string a_number);
}
public class Cell : ICellT9
{
private Dictionary<int, Cell> m_nodeHolder;
private List<string> m_nameList;
public Cell()
{
m_nodeHolder = new Dictionary<int, Cell>();
for (int i = 2; i < 10; i++)
{
m_nodeHolder.Add(i, null);
}
}
public void Add(string a_name)
{
Add(a_name, a_name);
}
private void Add(string a_name, string a_originalName)
{
if ((string.IsNullOrEmpty(a_name) == true) && (string.IsNullOrEmpty(a_originalName) == false))
{
if (m_nameList == null)
{
m_nameList = new List<string>();
}
m_nameList.Add(a_originalName);
}
else
{
int l_firstNumber = CharToNumber(a_name[0].ToString());
if (m_nodeHolder[l_firstNumber] == null)
{
m_nodeHolder[l_firstNumber] = new Cell();
}
m_nodeHolder[l_firstNumber].Add(a_name.Remove(0, 1), a_originalName);
}
}
public List<string> GetNames(string a_number)
{
List<string> l_result = null;
if (string.IsNullOrEmpty(a_number))
{
return l_result;
}
int l_firstNumber = a_number[0] - '0';
if (a_number.Length == 1)
{
l_result = m_nodeHolder[l_firstNumber].m_nameList;
}
else if(m_nodeHolder[l_firstNumber] != null)
{
l_result = m_nodeHolder[l_firstNumber].GetNames(a_number.Remove(0, 1));
}
return l_result;
}
private int CharToNumber(string c)
{
int l_result = 0;
if (c == "a" || c == "b" || c == "c")
{
l_result = 2;
}
else if (c == "d" || c == "e" || c == "f")
{
l_result = 3;
}
else if (c == "g" || c == "h" || c == "i")
{
l_result = 4;
}
else if (c == "j" || c == "k" || c == "l")
{
l_result = 5;
}
else if (c == "m" || c == "n" || c == "o")
{
l_result = 6;
}
else if (c == "p" || c == "q" || c == "r" || c == "s")
{
l_result = 7;
}
else if (c == "t" || c == "u" || c == "v")
{
l_result = 8;
}
else if (c == "w" || c == "x" || c == "y" || c == "z")
{
l_result = 9;
}
return l_result;
}
}

How to set default display value for data gridview?

I am using Data Grid View.
I have a list of object lets say.
class abc{
public int i{get;set;}
public long b{get;set;}
}
//in Form Load
List<abc> objList = new List();
listpopulate(); // populate the list.
datgridvew.dtasource = objList;
I want to show "-" where Objabc.i value = 0
and want to show "Good" where Objabc.b = 1000
I tried cell default format but in vain.
Can you please help me out ?
You can use an event for that, like:
DataGridView.CellValueChanged Event
Or use DataGridView.CellFormatting Event like this:
private void DataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "i" &&
e.RowIndex >= 0 &&
dgv["i", e.RowIndex].Value is int) &&
(((int)dgv["i", e.RowIndex].Value) == 0)
{
e.Value = "-";
e.FormattingApplied = true;
}
}
else if (dgv.Columns[e.ColumnIndex].Name == "b" &&
e.RowIndex >= 0 &&
dgv["b", e.RowIndex].Value is int) &&
(((int)dgv["b", e.RowIndex].Value) == 1000)
{
e.Value = "Good";
e.FormattingApplied = true;
}
}
}
HEre you can do using Jquery on clientside
$(document).ready(function () {
$(".GRIDVIEWCLASSNAME").find("td").each(function () {
if ($(this).text() == "0")
{
$(this).html("-");
}
if ($(this).text() == "1000")
{
$(this).html("Good");
}
});
});

Categories

Resources