|
Don't use CellPainting - use RowPostPaint:
private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
Rectangle rect = dgv.GetRowDisplayRectangle(e.RowIndex, false);
int leftRow = dgv.Columns["Col_B"].Index;
int rightRow = dgv.Columns["Col_D"].Index;
int leftX = dgv.GetCellDisplayRectangle(leftRow, e.RowIndex, true).X;
int rightX = dgv.GetCellDisplayRectangle(rightRow, e.RowIndex, true).X
+ dgv.GetCellDisplayRectangle(rightRow, e.RowIndex, true).Width;
e.Graphics.DrawLine(Pens.Red, new Point(leftX, rect.Top), new Point(leftX, rect.Bottom));
e.Graphics.DrawLine(Pens.Red, new Point(rightX, rect.Top), new Point(rightX, rect.Bottom));
}
}
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you. It works like a charm.
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
I want to create a program that will get data from an excel sheet shows in 4 combo box with a drop drown list. For example, if I choose menu1, drop list will the corresponding submenu (i.e, submenu11 & submenu12) rather than showing all other submenus. Again, once selecting submenu11, drop down list will show only the child111 & child112.
I have already imported the data from excel but unable to sort data in a combo box.
https://i.stack.imgur.com/6ixOP.png[^]
https://i.stack.imgur.com/LzZdn.png[^]
|
|
|
|
|
You need to be able to "relate" the combo boxes (data).
Parent3->parent2->parent1->child
parent2 is filtered (LINQ where) on parent3 "selected" item.
parent1 is filtered on parent2 selected item.
etc.
They can all use the same collection: p3 "full" view; p2 view limited to p3 selected item; etc.
(If no specific selected item, default to first in each "view").
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Thanks for your reply. But i have not so much knowledge in c#. If you give some coding help then it's very helpful for me
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you, of course, I will do my part. I just ask you if you have any coding idea then please help me. I don't tell you to do my full project.
actually, I don't have any good c# programming knowledge I'm trying to learn from the internet. And this is not my school or work project.
I already read excel using OleDb and tried LINQ to shorting data but I still not able to.
|
|
|
|
|
And there you are making a huge mistake.
Stop trying to "learn from the internet" and get a good book (or better go on a course) - Addison Wesley do some good ones, as do Wrox and Microsoft. Even download this: Dot Net Zero[^] which will give you a lot of it.
The problem is that learning from the internet is pretty much random - and if any of that is YouTube then most of it is produced by people who know little (if any) more than you do, and have no idea who to make a video or teach. So you miss a lot of stuff that you don't know that you need to know - like SQL Injection in the Bill thread code. You are trying to run before you can walk - and that's a porr way to get started.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If you've loaded the menu items from the spreadsheet, there must be some indicator in the data telling you which child item belongs to which parent item. If you know that one thing, building a hierarchical menu is pretty easy.
Beyond that, you haven't indicated what your data looks like, so we can't possibly help you any further.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Load each "child" LISTVIEW (query menu item collection) as the parent's listview "selection changed" event fires. Cascades to the lowest level.
This is an example of (hand-rolled) "data virtualization".
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
You need to start by describing the structure/format in which the data is available in C#. Is it in a DataTable ? a CSV file ? a 2d array ? or ... ?
You can, of course, fill the top-level ComboBox when the Form loads, since its contents do not change.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 10-Jun-19 5:31am.
|
|
|
|
|
Thanks for your reply. But i have not so much knowledge in c#. If you give some coding help then it's very helpful for me
|
|
|
|
|
I'll be happy to assist you, but, if you can't tell me what format the data imported from Excel is in ... or, show me the working code you have now that imports the data: I can't offer any useful advice.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I imported from excel using OleDb. now I can't be shorting data. this code now I'm trying..
string stringconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tbxSelect.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(stringconn);
if (tbxSelect.Text != "")
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + tbxChoice.Text + "$]", conn);
DataTable dt = new DataTable();
dt.Clear();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
string firstRow = dt.Rows[i]["A"].ToString();
if (firstRow != "")
{
cmbRow1.Items.Add(firstRow);
if (!firstRow.Equals(Row1))
{
RowIndex_1++;
}
Row1 = firstRow;
}
MessageBox.Show(i.ToString(),RowIndex_1.ToString());
}
for (int j = 0; j < dt.Rows.Count; j++)
{
string secondRow = dt.Rows[j]["B"].ToString();
if (secondRow != "")
{
cmbRow2.Items.Add(secondRow);
if (!secondRow.Equals(Row2))
{
RowIndex_2++;
}
Row2 = secondRow;
}
MessageBox.Show(j.ToString(), RowIndex_2.ToString());
}
for (int k = 0; k < dt.Rows.Count; k++)
{
string secondRow = dt.Rows[k]["C"].ToString();
if (secondRow != "")
cmbRow3.Items.Add(secondRow);
}
for (int l = 0; l < dt.Rows.Count; l++)
{
string secondRow = dt.Rows[l]["D"].ToString();
if (secondRow != "")
cmbRow4.Items.Add(secondRow);
}
for (int m = 0; m < dt.Rows.Count; m++)
{
string secondRow = dt.Rows[m]["E"].ToString();
if (secondRow != "") ;
}
MessageBox.Show((dt.Rows.Count).ToString());
conn.Close();
}
else
MessageBox.Show("Er");
|
|
|
|
|
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood' The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Note: it's possible the Excel range you show has 'split row; and, there are other ways you can create simulated hierarchies in Excel: that's why is crucial to examine the structure of the DataTable.
You need to do this: insert this code after the line beginning: da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
Console.Write($"row: {i}\t");
for (int j = 0; j < dt.Columns.Count; j++)
{
Console.Write($"{row[j]}\t");
}
Console.WriteLine();
} Put a break-point just after this inserted code. Run the code. When it stops at the break-point, copy the output in the Console Window, and save it so you can study it. Post it here.
Study the structure revealed, and start thinking of what has to happen to map that to the ComboBoxes.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Thank you! I think it is a great idea. I will try and let's see
|
|
|
|
|
Data comes with this format. what is a good way to rearrange it
row: 0 menu1 submenu11 child111 item1111
row: 1 item1112
row: 2 child112 item1121
row: 3 item1122
row: 4 item1123
row: 5 submenu12 child121 item1211
row: 6 child122 item1221
row: 7 item1222
row: 8 menu2 submenu21 child211 item2111
row: 9 item2112
row: 10 item2113
row: 11 child212 item2121
row: 12 child213 item2131
row: 13 submenu22 child221 item2211
row: 14 menu3 submenu31 child311 item3111
row: 15
row: 16
row: 17
row: 18
row: 19
row: 20
|
|
|
|
|
That's an important step: start thinking about how you would use the 'menu, 'submenu, 'child, and 'item labels to create the tree structure.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
To be sure the structure shown is accurate, we need to check for the presence of "DBNull" values: please run this code and show the result:
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
Console.Write($"row: {i}\t");
for (int j = 0; j < dt.Columns.Count; j++)
{
var cvalue = row[j];
string cstr = cvalue is DBNull ? "DBNull" : row[j].ToString();
Console.Write($"{cstr}\t");
}
Console.WriteLine();
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I able to solve the problem... thank you very much
i just scan ever row then match its child row. like this
menu1---submenu11---child111----item1111
menu1---submenu11---child111----item1112
menu1---submenu11---child112----item1121
|
|
|
|
|
Overly simplified:
public class MyMenuItem
{
public MyMenuItem Parent { get; set; }
public string Header { get; set; }
public List<MyMenuItem> Children { get; set; }
public MyMenuItem(string header, MyMenuItem parent=null)
{
this.Parent = parent;
this.Header = header;
this.Children = new List<MyMenuItem>();
}
}
List<MyMenuItem> MenuItems = new List<MyMenuITem>();
Populate the MenuItems appropriately, and have a ball. I'm not sure why you're putting menu items into comboboxes, but whatever...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|