c# getting specified cell (3rd column of last row) edited - c#

I have posted a topic about this earlier, but since I have made some mistakes so I decided to post a new topic to make sure that my question is clear enough.
Here's my code :
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
int nRowIndex = dataGridView1.Rows.Count-1;
if (dataGridView1.Rows[nRowIndex].Cells[2].Value != null)
{
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells[2].Value.ToString();
}
else
{
MessageBox.Show("NULL");
}
}
}
In my DataGridView, I have 3 columns which are ID, Name, Price.
I want to get the last cell of Price which is at the last row of the DataGridView.
The above code causes me to have NullReferenceException and the specified cell does have data in it.
Anyone know how to solve this problem?

Try this:
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells["Price"].Value.ToString();
and report please.

Related

Can't change color of specific rows in a DataGridView. DefaultCellStyle.BackColor not working

I'm trying to change the color of some specific rows in a DataGridView, but for some reason DefaultCellStyle.BackColor is not working. If I change the color of the whole table, it works. But if I try to do it to a single row, it just doesn't work. This is the code I wrote:
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToString(row.Cells[20].Value) != "")
{
MessageBox.Show("Inside if"); //With this messagebox I make sure I get inside the if.
row.DefaultCellStyle.BackColor = Color.Red;//This just doesn't work.
}
}
I also tried to change the color of just one row, but had no luck.
dataGridView2.Rows[1].DefaultCellStyle.BackColor = Color.Red;
I don't know what am I missing or if I'm just too dumb to see the problem. I've already searched in different forums for an answer but I couldn't find any.
The datagridview control cannot change colors until the form has been shown. If you have this in the Load event, move it to the Shown event.
OR
You can try putting it in the DataGridView_CellFormatting event like this:
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// Assuming 20 is the column index you're looking for
if (this.dataGridView2.Columns[e.ColumnIndex].Index == 20)
{
if (e.Value != null)
{
string strVal = (string)e.Value;
if (strVal != "")
{
e.CellStyle.BackColor = Color.Red;
}
}
}
}

WPF DataGrid ColumnCount

I have a DataGrid with a DataTable as DataGrid.ItemsSource.
So far everything works fine and the Data is shown in the DataGrid as i want it to be.
Now I want to hide some of the Columns of the DataGrid. And I have done this before and it worked fine but somehow I always get an Error saying
"System.ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the size of the collection."
I know what this means but I don't understand why this is happening.
Here's my code:
adapter.Fill(datatable);
NameDG.ItemsSource = datatable.DefaultView;
//Hide Column[1]
NameDG.Columns[1].Visibility = Visibility.Hidden;
The DataGrid has more then 10 Columns.
Thanks for help.
if you click the column name it's have a negative value specific value is -1 you need to validate it by using IF STATEMENT
use this event to prevent the error
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
if(rowIndex <= -1)
{
//Error
}
else
{
//Your Code
}
}
Just to close this thread I will answer the question myself after I found the solutions.
You can delet the Columns directly from the DataTable like for example through this (Thanks to Nobody)
datatable.Columns.Remove(datatable.Columns[0]);
or
Use the AutoGeneratedColumns event handler from the DataGrid
You can try
NameDG.Loaded += NameDG_Loaded;
void NameDG_Loaded(object sender, RoutedEventArgs e)
{
NameDG.Columns[1].Visibility = Visibility.Collapsed;
}

How to hide Rows In GridView

I have came across the we and check different web pages but I didnt find the one I was looking for. I have a gridview and all I want is to hide one of the rows based on the value in the cell.
What I need to happen is something like in the logic of this :
if (row = "someValue")
{
row.Visible = false;
}
for the record, I have tried this but no luck:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string oRoleName = row.Field<string> ("SVal");
if (oRoleName.Equals ("someValue")) {
e.Row.Visible = false;
}
}
It is not base weather the row is the first the 2nd or third (like: e.row[1], e.row[2], etc.) I need to filter the data base on the value in the row. Can anyone teach me how could this be done ?
Would appreciate any help.
In a RowDataBound event add something along the lines of this logic
if (e.Row.Cells[5].Text == "foo") {
e.Row.Visible = false;
}
EDIT:
If youre looking to check the value of each row as its entered (unless im understanding incorrectly, you should probably expand on your question a bit.)
Then you may want to use the event "CellValueChanged"
Check to see if the cell is null beforehand and then do the check for your value and apply logic accordingly below that.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell != null) {
if (dataGridView1.CurrentCell.Value.ToString() == "foo")
{
// do your stuff here.
}
}
}
What you need to do is get the value of the column within the GridViewRow. You are on the right track. In RowDataBound, find the column you are after. Then check its value.
Here is an example. Use FindControl() to get the control in the specified column for the current row. If the control in that column is a Label, check the text of the label to see if it is the value you want hidden. If so, hide the row.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("MyLabel");
if (lbl.Text == "MyValue")
{
e.Row.Visible = false;
}
}
}

DevComponents DotNetBar SuperGridControl: How to change just 1 row's background color

As the subject indicates, I have a DevComponents DotNetBar SuperGridControl (SGC) on my Windows Form. In that SGC, I have alternating row colors. One of the columns in the SGC has boolean values (enabled/disabled flag in the data).
I would like to change the background color of JUST the rows that are marked with a false boolean value.
The code I've attempted to use to perform this task:
private void dgvSearchResults_PostRenderRow(object sender, GridPostRenderRowEventArgs e)
{
if (e.RenderParts != RenderParts.Background) { return; }
var row = (GridRow)e.GridRow;
if (((CustomerDTO)row.DataItem).Disabled)
{
//Try to figure out how to set the row color here.
}
}
The nasty part of this is that this code apparently runs twice for every row in the SGC. But, that part aside, there doesn't seem to be any way to change the row color of the row that I'm in when I get into the .Disabled control statement.
I'd love any tips or suggestions.
So I actually did sort out an answer to this one, and it turns out it was fairly simple. I was just calling the wrong event handler. The below code performs the exact task I was attempting in my initial question:
private void dgvSearchResults_GetRowCellStyle(object sender, GridGetRowCellStyleEventArgs e)
{
if (e.StyleType != StyleType.Default) { return; }
var row = e.GridRow as GridRow;
if (row == null) { return; }
if (((CustomerDTO)row.DataItem).Disabled) {
e.Style.Background = new Background(Color.Tomato);
}
}

Loading certain data in combobox based on textbox input

Based on gender, I want to load certain criteria.
For example, if I type 0 in a textbox, I want to load Mr., Dr. , etc..
If I type 1, I want to load Ms, Mrs, Miss, Dr. etc...
How can I do so?
Gender is typed in a textbox, and I want the combo box to load what I specified above.
Thank you.
This is just Sudo code there must be typo or Syntax error but you need to do something as below :
List<string> strMale = new List<string>{"Mr.", "Dr. "};
List<string> strFMale = new List<string>{"Mrs.", "Miss"};
//make use of Textbox Change Event
public void Text1_TextChanged(object sender, EventArgs e)
{
Combo1.Items.Clear();
//Bind the values using the text box input value
if(Text1.Text=="0")
{
Combo1.DataSource = strMale ;
}
else if(Text1.Text=="1")
{
Combo1.DataSource = strFMale ;
}
Combo1.SelectedIndex = 0;
}
You have to handle event ValueChanged (the name of the event depends on platform you're working with) and depending on value typed change source of your combobox
Try the following Code Use can use comboBox.Items.Insert or comboBox.Items.Add for inserting new items into a combobox.
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text == "0")
{
if (comboBox1.Items.Count > 0)
comboBox1.Items.Clear();
comboBox1.Items.Insert(0,"Mr");
comboBox1.Items.Insert(1, "Dr");
}
else if (textBox2.Text == "1")
{
if (comboBox1.Items.Count > 0)
comboBox1.Items.Clear();
comboBox1.Items.Add("Ms");
comboBox1.Items.Add("Mrs");
comboBox1.Items.Add("Miss");
}
}

Categories

Resources