ANINDYA
asked on
datagridview cell click of null value
Experts
What I have to write here to avoid the errors inside the if loop
public void dataGridView5_CellClick(ob ject sender, DataGridViewCellEventArgs e)
{
if ((dataGridView5.CurrentRow .Cells[2]. Value != null)||(dataGridView5.Curr entRow.Cel ls[2].Valu e.ToString () != "0"))
{
filmid = Convert.ToInt32(dataGridVi ew5.Curren tRow.Cells [2].Value) ;
dt = Convert.ToDateTime(dataGri dView5.Cur rentRow.Ce lls[5].Val ue);
InsertIntoList(filmid, dt);
}
else
{
MessageBox.Show("There is no Item Borrowed.","No Item Borrowed",MessageBoxButton s.OK,Messa geBoxIcon. Exclamatio n);
}
}
For further clarification of my question please refer the images attached
Thanking you
error.JPG
1.JPG
What I have to write here to avoid the errors inside the if loop
public void dataGridView5_CellClick(ob
{
if ((dataGridView5.CurrentRow
{
filmid = Convert.ToInt32(dataGridVi
dt = Convert.ToDateTime(dataGri
InsertIntoList(filmid, dt);
}
else
{
MessageBox.Show("There is no Item Borrowed.","No Item Borrowed",MessageBoxButton
}
}
For further clarification of my question please refer the images attached
Thanking you
error.JPG
1.JPG
ASKER
Expert aymoon80
I have done as per your suggestion and it is working perfectly .
Thanks for your suggestion and help.
Sir I have a small question
Why you asked me to use that CellEndEdit Event?
Thanking you
Anindya
I have done as per your suggestion and it is working perfectly .
Thanks for your suggestion and help.
Sir I have a small question
Why you asked me to use that CellEndEdit Event?
Thanking you
Anindya
ASKER
Sir
The cell click is necessary as otherwise I would not get the values of the
filmid and dt as you can see in the code.
Please help
Thanking you
Anindya
The cell click is necessary as otherwise I would not get the values of the
filmid and dt as you can see in the code.
Please help
Thanking you
Anindya
Thanks, I ask you to use CellEndEdit event to handle the data after changes not before changing it.
I'll give you the code to get the cell value later to check this by myself.
Regards,
Ayman
I'll give you the code to get the cell value later to check this by myself.
Regards,
Ayman
but what is the datasource of your datagrid is that a datatable or other thing?
ASKER
Expert aymoon80
It is a data table.
As you can see the code here I have shown in the attached image.
Please see.
private void Form_Search_Items_Borrowed _Load(obje ct sender, EventArgs e)
{
//string query = "select * from Table_Borrow where MemberID=@parameter1";
string b = "Borrowed";
string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-6FBADA\\SQ LEXPRESS;I nitial Catalog=AnindyaCD;Integrat ed Security=SSPI"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValu e("@parame ter1", MemberID);
using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
{
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds, "Table_Borrow");
dataGridView5.DataSource = ds.Tables[0];
}
}
}
}
public Int32 filmid { get; set; }
public DateTime dt { get; set; }
//public static Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
//public static Dictionary<int, DateTime> FilmSelectDictionary {get;set;}
Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
public Dictionary<int, DateTime> FilmSELECTDictionary
{
get { return FilmSelectDictionary; }
}
public void dataGridView5_CellClick(ob ject sender, DataGridViewCellEventArgs e)
{
try
{
if ((dataGridView5.CurrentRow .Cells[2]. Value != null) || (dataGridView5.CurrentRow. Cells[2].V alue.ToStr ing() != "0"))
{
filmid = Convert.ToInt32(dataGridVi ew5.Curren tRow.Cells [2].Value) ;
dt = Convert.ToDateTime(dataGri dView5.Cur rentRow.Ce lls[5].Val ue);
InsertIntoList(filmid, dt);
}
else
{
MessageBox.Show("There is no Item Borrowed.", "No Item Borrowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToStrin g());
}
}
It is a data table.
As you can see the code here I have shown in the attached image.
Please see.
private void Form_Search_Items_Borrowed
{
//string query = "select * from Table_Borrow where MemberID=@parameter1";
string b = "Borrowed";
string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-6FBADA\\SQ
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValu
using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
{
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds, "Table_Borrow");
dataGridView5.DataSource = ds.Tables[0];
}
}
}
}
public Int32 filmid { get; set; }
public DateTime dt { get; set; }
//public static Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
//public static Dictionary<int, DateTime> FilmSelectDictionary {get;set;}
Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
public Dictionary<int, DateTime> FilmSELECTDictionary
{
get { return FilmSelectDictionary; }
}
public void dataGridView5_CellClick(ob
{
try
{
if ((dataGridView5.CurrentRow
{
filmid = Convert.ToInt32(dataGridVi
dt = Convert.ToDateTime(dataGri
InsertIntoList(filmid, dt);
}
else
{
MessageBox.Show("There is no Item Borrowed.", "No Item Borrowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToStrin
}
}
private void Form_Search_Items_Borrowed_Load(object sender, EventArgs e)
{
//string query = "select * from Table_Borrow where MemberID=@parameter1";
string b = "Borrowed";
string query = "select * from Table_Borrow where MemberID=@parameter1 and Status LIKE '" + b.ToString() + "' ";
using (SqlConnection con = new SqlConnection("Data Source=INVENTOR-6FBADA\\SQLEXPRESS;Initial Catalog=AnindyaCD;Integrated Security=SSPI"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@parameter1", MemberID);
using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
{
ad.SelectCommand = cmd;
DataSet ds = new DataSet();
ad.Fill(ds, "Table_Borrow");
dataGridView5.DataSource = ds.Tables[0];
}
}
}
}
public Int32 filmid { get; set; }
public DateTime dt { get; set; }
//public static Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
//public static Dictionary<int, DateTime> FilmSelectDictionary {get;set;}
Dictionary<int, DateTime> FilmSelectDictionary = new Dictionary<int, DateTime>();
public Dictionary<int, DateTime> FilmSELECTDictionary
{
get { return FilmSelectDictionary; }
}
public void dataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if ((dataGridView5.CurrentRow.Cells[2].Value != null) || (dataGridView5.CurrentRow.Cells[2].Value.ToString() != "0"))
{
filmid = Convert.ToInt32(dataGridView5.CurrentRow.Cells[2].Value);
dt = Convert.ToDateTime(dataGridView5.CurrentRow.Cells[5].Value);
InsertIntoList(filmid, dt);
}
else
{
MessageBox.Show("There is no Item Borrowed.", "No Item Borrowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
error.JPG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the event name CellValidating event of your datagrid also you can switch on Column Index to make many validations.
also you can use Column name instead of Column Index to avoid problems when changing the columns order in your query.
also you can use Column name instead of Column Index to avoid problems when changing the columns order in your query.
you can use the same technique in dataGridView5_CellClick
ASKER
Expert aymoon80
Thanking you very much for the total code and also the help which you have rendered towards me .
i am delighted.
Thanks for your patience and valuable time.
Thanking you,
Anindya Chatterjee
Bangalore
India
Thanking you very much for the total code and also the help which you have rendered towards me .
i am delighted.
Thanks for your patience and valuable time.
Thanking you,
Anindya Chatterjee
Bangalore
India
ASKER
Expert aymoon80
I am thankful to you .
I have a request that is if in future I have questions can you help me.
If yes then please let me know how to provide you the URL of my question .
I want to provide you the urls and if you find a trifle time from your busy schedule then you may answer them.
Thanking you
Anindya Chatterjee
Bangalore
India
I am thankful to you .
I have a request that is if in future I have questions can you help me.
If yes then please let me know how to provide you the URL of my question .
I want to provide you the urls and if you find a trifle time from your busy schedule then you may answer them.
Thanking you
Anindya Chatterjee
Bangalore
India
Of course, I'm following your activities on experts-exchange so I'll follow your questions here.
Best Regards,
Ayman
Best Regards,
Ayman
then put:
try and catch
try
{
//your if condition and code
}
catch(Exception exp)
{
}
//you can put a break point to get the value of Cell[x] to handle it's exception