C
C#•4mo ago
Vegas

DataGrid Cell Selection / Row Color

So I have been fighting this for a bit and Stack is not helping 😦 What im looking for is if a certain column in the database is 1 then have the datagrid show red background.
No description
6 Replies
oke
oke•4mo ago
can you give more context? (a bigger picture of the code) because from what i see, the issue is that a DataGridView doesnt have a member called Cell what is does have is Rows[x].Cells because a DataGridView contains rows and columns, you need to specify the row, then you can select the cell from that so, maybe something like this?
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

// Not sure what 'dr' is
string can = dr[0].ToString();
if (can == "1" && dataview.Rows[0].Cells[26].Value as int? == 1)
{
dataview.RowBackground = Brushes.LightPink;
}
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

// Not sure what 'dr' is
string can = dr[0].ToString();
if (can == "1" && dataview.Rows[0].Cells[26].Value as int? == 1)
{
dataview.RowBackground = Brushes.LightPink;
}
im not too sure though someone who has more experience with c# than me might swing by and tear my code apart and better fix your issue
Vegas
Vegas•4mo ago
c#
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string can = dr[0].ToString();
if (can == "1")
{
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

// Not sure what 'dr' is
string can = dr[0].ToString();
if (can == "1" && dataview.Rows[0].Cells[26].Value as int? == 1)
{
dataview.RowBackground = Brushes.LightPink;
}
}
}
}
c#
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string can = dr[0].ToString();
if (can == "1")
{
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

// Not sure what 'dr' is
string can = dr[0].ToString();
if (can == "1" && dataview.Rows[0].Cells[26].Value as int? == 1)
{
dataview.RowBackground = Brushes.LightPink;
}
}
}
}
Vegas
Vegas•4mo ago
No description
Vegas
Vegas•4mo ago
Compiler Error CS1061 Does not contain definition.
oke
oke•4mo ago
can you give more? like the entire method? that, plus what type dataview is, because im led to assume its DataGridView
Vegas
Vegas•4mo ago
C#

string connetionString = @"Data Source= " + ServerName + ";Initial Catalog= " + Database + ";User ID=" + Username + ";Password= " + Password + ";";
using (SqlConnection conn = new SqlConnection(connetionString))
{
string lbcompjob = ("SELECT * FROM " + Table + " WHERE [cancled] = 1");
SqlCommand cmd = new SqlCommand(lbcompjob, conn);
{
try
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string can = dr[0].ToString();
if (can == "1")
{
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

}
}
}
}
catch (Exception ex)
{

}
}
}
C#

string connetionString = @"Data Source= " + ServerName + ";Initial Catalog= " + Database + ";User ID=" + Username + ";Password= " + Password + ";";
using (SqlConnection conn = new SqlConnection(connetionString))
{
string lbcompjob = ("SELECT * FROM " + Table + " WHERE [cancled] = 1");
SqlCommand cmd = new SqlCommand(lbcompjob, conn);
{
try
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string can = dr[0].ToString();
if (can == "1")
{
if (dr.Length == 0 || dataview.Rows.Count == 0 || dataview.Rows[0].Cells.Count < 26)
{
// The grid doesn't have enough rows or cells to access data in Row 1 Cell 26
Console.WriteLine("Cell 26 doesn't exist!");
return;
}

}
}
}
}
catch (Exception ex)
{

}
}
}
dataview is the datagrid
Want results from more Discord servers?
Add your server