|
Stanciu Vlad wrote: secondTableBindingSource.DataSource = tblSecondary;
This results in creating a table in the dataGridView that has i Rows + 1 (the +1 is the default row for the next entry), which are all blank. In this case it adds the 6 rows that correspond to 'tblSecondary.Rows.Add', but the rows added are blank. The first comboBox does allow you to drop down the three selections, but the row will remain blank. However, the filter will eliminate all rows except for a number of rows pertaining to the number of items in the filtered 'Units' column. But again they are blank, but you can at least select one and have it write to the cell. And last, but not least, selecting on the next row, 1st column comboBox throws an "Object reference not set to an instance of an object".
Unless you see something obvious here and can prod me in that direction, I have spent way too much time on trying to get this to work and it probably makes sense to throw out what I've tried and start from scratch. I thought this was going to be a bit more straightforward, but alas......
|
|
|
|
|
Check this out...
private void Form1_Load(object sender, EventArgs e)
{
DataTable mainData = new DataTable("mainData");
mainData.Columns.Add("ID", typeof(int));
mainData.Columns.Add("ValueType", typeof(int));
mainData.Columns.Add("ValueUnitOfMeasurement", typeof(int));
mainData.Columns.Add("TheValue", typeof(decimal));
DataTable valueType = new DataTable("valueType");
valueType.Columns.Add("ID", typeof(int));
valueType.Columns.Add("Description", typeof(string));
DataTable valueUnitOfMeasurement = new DataTable("valueUnitOfMeasurement");
valueUnitOfMeasurement.Columns.Add("ID", typeof(int));
valueUnitOfMeasurement.Columns.Add("ValueTypeID", typeof(int));
valueUnitOfMeasurement.Columns.Add("Description", typeof(string));
valueType.Rows.Add(new object[] { 1, "Force" });
valueType.Rows.Add(new object[] { 2, "Torque" });
valueType.Rows.Add(new object[] { 3, "Pressure" });
valueUnitOfMeasurement.Rows.Add(new object[] { 1, 1, "lb"});
valueUnitOfMeasurement.Rows.Add(new object[] { 2, 1, "N" });
valueUnitOfMeasurement.Rows.Add(new object[] { 3, 2, "N-m" });
valueUnitOfMeasurement.Rows.Add(new object[] { 4, 2, "in-lb" });
valueUnitOfMeasurement.Rows.Add(new object[] { 5, 3, "Pa" });
valueUnitOfMeasurement.Rows.Add(new object[] { 6, 3, "bar" });
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = mainData;
valueTypeColumn.DataSource = valueType;
valueTypeColumn.ValueMember = "ID";
valueTypeColumn.DisplayMember = "Description";
myBinding.DataSource = valueUnitOfMeasurement;
valueUnitOfMeasurementColumn.DataSource = myBinding;
valueUnitOfMeasurementColumn.ValueMember = "ID";
valueUnitOfMeasurementColumn.DisplayMember = "Description";
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
dataGridView1.RowValidated += new DataGridViewCellEventHandler(dataGridView1_RowValidated);
}
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
myBinding.Filter = string.Empty;
}
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox editingControl = e.Control as ComboBox;
if (editingControl.DataSource is BindingSource)
{
if ((editingControl.DataSource as BindingSource).Equals(myBinding))
{
if(dataGridView1.CurrentRow == null)
return;
int selectedID;
bool ok = Int32.TryParse(Convert.ToString(dataGridView1.CurrentRow.Cells["valueTypeColumn"].Value), out selectedID);
if (ok)
myBinding.Filter = "ValueTypeID = " + selectedID;
else
myBinding.Filter = "ValueTypeID = -1";
return;
}
}
}
}
I have no smart signature yet...
|
|
|
|
|
Apparently you took a look at the code I had in the previous post and said "enough is enough" or "I'll never be able to help that"??
A couple of questions:
Stanciu Vlad wrote: DataTable mainData = new DataTable("mainData");
mainData.Columns.Add("ID", typeof(int));
mainData.Columns.Add("ValueType", typeof(int));
mainData.Columns.Add("ValueUnitOfMeasurement", typeof(int));
mainData.Columns.Add("TheValue", typeof(decimal));
I can't see that this is doing anything accept providing a DataSource place marker for dataGridView1. In fact, all of the mainData.Columns.Add lines can be commented out without impacting the functionality.
In either case, it doesn't take long to add different types of measurements (Force, Torque, or Pressure) to have it begin to throw exceptions. Would you concur with this?
Thanks.
|
|
|
|
|
mainData is the place where the data is stored. I said that you could persist this in a database. So if you did that then your table would look like that: with a reference to the ValueType table and a referece to the ValueUnitOfMeasurement on each row.
If you comment out all the mainData columns you would not have a place where to store the data entered in the grid and the grid would be empty.
So, if you add different types of measurements and do not bind them correctly the application will crash. In all the tables the ID column must be unique (numbers from 1 to n). In the valueUnitOfMeasurement table there is an ID column (that must be unique) and a reference to a valueType (stated in the valueType table). The reference is the ID of the valueType.
Let's assume you want to add a new measurement type named "Distance" with the units of measurement "km", "m", "cm", "mm".
The first step is to add the record in the ValueType table:
valueType.Rows.Add(new object[] {100, "Distance"});
Note that the row added is [100, "Distance"] - 100 is the Distance's ID (must be unique in the table).
Next you must add records in the ValueUnitOfMeasurement table:
valueType.Rows.Add(new object[] {500, 100, "km"});
valueUnitOfMeasurement.Rows.Add(new object[] {501, 100, "m"});
valueUnitOfMeasurement.Rows.Add(new object[] {502, 100, "cm"});
valueUnitOfMeasurement.Rows.Add(new object[] {503, 100, "mm"});
Note that the first row added is [500, 100, "km"] - 500 is the UnitOfMeasurement's ID and 100 is the valueType's ID correspondind to this unit of measurement (A couple of lines above we stated that for distance 100 is the ID).
The logic in my previous example filters the unit of measurements based on the selected value type ID in the valueType combo box. This supports an logically unlimited number of measurement types and units of measure. Also this supports persistance (database or xml).
I have no smart signature yet...
|
|
|
|
|
from:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/6f6c2632-afd7-4fe9-8bf3-c2c8c08d1a31/[^]
public partial class Form1 : Form
{
DataTable tblPrimary, tblSecondary;
BindingSource primaryBS, filteredSecondaryBS, unfilteredSecondaryBS;
public Form1()
{
tblPrimary = new DataTable("Primary");
tblPrimary.Columns.Add("ID", typeof(int));
tblPrimary.Columns.Add("Name", typeof(string));
tblSecondary = new DataTable("Secondary");
tblSecondary.Columns.Add("ID", typeof(int));
tblSecondary.Columns.Add("subID", typeof(int));
tblSecondary.Columns.Add("Name", typeof(string));
tblPrimary.Rows.Add(new object[] { 0, "Force" });
tblPrimary.Rows.Add(new object[] { 1, "Torque" });
tblPrimary.Rows.Add(new object[] { 2, "Pressure" });
tblSecondary.Rows.Add(new object[] { 0, 0, "lb" });
tblSecondary.Rows.Add(new object[] { 1, 0, "N" });
tblSecondary.Rows.Add(new object[] { 2, 0, "oz" });
tblSecondary.Rows.Add(new object[] { 3, 1, "in-lb" });
tblSecondary.Rows.Add(new object[] { 4, 1, "ft-lb" });
tblSecondary.Rows.Add(new object[] { 5, 1, "N-m" });
tblSecondary.Rows.Add(new object[] { 6, 2, "PSI" });
tblSecondary.Rows.Add(new object[] { 7, 2, "Pa" });
tblSecondary.Rows.Add(new object[] { 8, 2, "bar" });
InitializeComponent();
primaryBS = new BindingSource();
primaryBS.DataSource = tblPrimary;
primaryComboBoxColumn.DataSource = primaryBS;
primaryComboBoxColumn.DisplayMember = "Name";
primaryComboBoxColumn.ValueMember = "ID";
unfilteredSecondaryBS = new BindingSource();
DataView undv = new DataView(tblSecondary);
unfilteredSecondaryBS.DataSource = undv;
secondaryComboBoxColumn.DataSource = unfilteredSecondaryBS;
secondaryComboBoxColumn.DisplayMember = "Name";
secondaryComboBoxColumn.ValueMember = "ID";
filteredSecondaryBS = new BindingSource();
DataView dv = new DataView(tblSecondary);
filteredSecondaryBS.DataSource = dv;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == secondaryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = filteredSecondaryBS;
this.filteredSecondaryBS.Filter = "subid = " +
this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.secondaryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = unfilteredSecondaryBS;
this.filteredSecondaryBS.RemoveFilter();
}
}
|
|
|
|
|
It's ok if it works. I's kind of based on the principle stated by me in a previous post. The filter logic is a little diferent, it's a different approach.
Just as a note:
1. a bindingSource does aproximatively the same thing as a dataView (if not the same), so there is no reason to bind to the bindingSource a dataView binded to a dataTable. You could bind the table directly to the bindingSource.
2. having a filtered and an unfiltered bindingSource is the same as having a bindingSource that can be filtered or unfiltered.
3. I think that if you change the datasource of a control the current selected value is lost (not sure, but this would seam logic to me)
I have no smart signature yet...
|
|
|
|
|
Were you not getting exceptions after adding a couple of Force rows and then select a Torque row?
|
|
|
|
|
I don't recall, but if in certain conditions you get an exception then you've found a bug
I have no smart signature yet...
|
|
|
|
|
Hi
I wanted to ask if it is possible to do something like this and how to do it:
I would like to have a xml structure like this:
<root>
<collection>
<element />
<element attribute="value" />
<element />
</collection>
<something />
<somethingother />
</root>
The elements in the collection should be of the type ElementBase if the attribute is not set or of the type ElementDerived if the attribute is set to a value (the second element).
So the Deserialized collection should be something like:
List<ElementBase> collection = new List<ElementBase>() { new ElementBase(), new ElementDerived(), new ElementBase() };
ElementDerived would be derived from ElementBase:
class ElementDerived : ElementBase {}
I hope my question is described clearly.
Thanks for your help
Roland
|
|
|
|
|
|
This is possible if you write your own custom xml deserializer.
Based on your xml rules build the deserialization rules and then, at the parsing of element nodes, check if there is an atribute defined, and based on the result instantiate your element class.
But I don't understand why you want to use something like this (inheritance), why not just put a flag in the base class (a bool flag, or an enum flag) to diferentiate between the two element types?
I have no smart signature yet...
|
|
|
|
|
i have an application in a machine and i want to make access on it through an other machine , how can i make that,plz help ?
|
|
|
|
|
it depends on what information you will be passing between the aplications, and your architecture - for example, if you have an application on a machine, lets call that the server side, how many other computers 'clients' are going to be connecting to the server and requesting services or transferring data
- I would start looking at remoteing, wcf type technologies - ie, you possibly need to start thinking about tcp/ip sockets type connectivity, and there are easy and hard ways to do that depending on your requirements.
Its harder to give you a better answer without a more detailed description of what you are trying to achieve, sorry
'g'
|
|
|
|
|
Hello,
Tell me more about the type of application you have. Is it a windows form, console application or a website?
Nicolas.
|
|
|
|
|
any way the application is depend on the distributed systems , so i want to just access to the other machine so you can say that i want to make remote desktop like that in windows ....
what can i do with c# ?
|
|
|
|
|
|
how to get number of records selected?
SqlDataReader dr1 = cmd1.ExecuteReader();
|
|
|
|
|
I don't think you can tell ahead of time how many records will be returned. If you need to know that, then you can use a COUNT function in your SQL or loop through the records and count them as you go.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
The other way will be to use a dataset and do a count of that dataset:
SqlDataAdapter sqldataadap = new SqlDataAdapter(objCmd); //objCmd is your Sql Command Object
DataSet ds = new DataSet();
sqldataadap.Fill(ds);
//get the # of rows:
int irow=ds.Tables[0].Rows.Count;
|
|
|
|
|
After running any type of query just fire following query to know
number of rows affected.
SELECT @@rowcount AS 'RowsChanged'
Hope this will help!
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Fill a data adapter with a dataset. Then count the rows.
Some thing like:
int totalRow = Dataset.Tables[0].Rows.Count;
|
|
|
|
|
The reader does not have a count.
You could however use an ExecuteScalar to get the count.
int i = (int)cmd1.ExecuteScalar();
|
|
|
|
|
How exactly do you reckon this will return the row count?
What is cmd1.CommandText in this case?
|
|
|
|
|
I have finished building an desktop application that is similar to Yahoo Messenger.
It has a client side and a server side and it is built using System.Runtime.Remoting
and uses a Singleton model.
I have registered a channel on the client side and on the server side like this:
ChannelServices.RegisterChannel(channel,false);
...
MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(IServer),
"http://"+hostIP+":8980/Server");
Global.server=obj as IServer;
(obj as RemotingClientProxy).Timeout=5000;
where hostIP an IP address of the server location.
Until now I have used only 127.0.0.1 (localhost) for the server , but when I change the value to
my IP address, I get the following error:
{"Unable to connect to the remote server"} System.Exception {System.Net.WebException}
with the message :
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"
Does anyone knows how to solve this issue? Should I configure in some way the server(my computer) ?
Please help me .
Thanks in advance!
|
|
|
|
|
Your firewall is probably on. Open port 8980 for the interface on which you are trying to connect.
|
|
|
|
|