开发者

C# Datagridview: Combobox Column cell value is null even after user selects item

I'm constructing a GUI that allows the user to manipulate XML data. A helpful fellow named Peter was able to point me to a direction leading me to question why my cell values were null even when the user selects a value in the combobox.

I've read up on a few things: the comboboxcolumn's value member, display member, data source, and datapropertyname. I've figured out what display and value members do, but decided to stick with a data source so I wouldn't have to specify a display member and value member; I believe it made things easier and plus the choices in the combobox are stored in a string array.

The premise is, the user enters in choices into a rich text box. Then, the textbox is read line by line and every line becomes a choice in the combo box. I then create a new combobox column in the datagridview and the choices are available for the user to choose. After the user is done, I have a button called "save with combo column..." which creates a new datacolumn in the datatable that the datagridview is displaying and I try to copy the values over via this code: (since I cannot directly merge the combobox column with a datatable)

            int size = dataGridView1.Rows.Count - 1;
            DataColumn column = new DataColumn(combo.HeaderText);
            data_set_array[(int)IndexNumber.Value].Tables[(int)TableNumber.Value].Columns.Add(column);
            for (q = 0; q < size; q++)
            {
                data_set_array[(int)IndexNumber.Value].Tables[(int)TableNumber.Value].Rows[q][combo.HeaderText] = dataGridView1["combo", q].Value;
            }

when that is all said and done, the new column is created and a message is displayed. However, the datatable's values are null, which means the cel开发者_运维问答l values are also null. This implies that the item that the user selected in the combo box wasn't copied over to the cell value. What am I doing wrong? I set the datasource to the string of arrays where the strings are the user's choices for the combobox, and yet when the I selected choices from the combobox and tried to save it, the values are null. I also read that I did not need to worry about value member or display member since setting the datasource would provide me valid display text and valid values.

If any additional information is needed please ask. Thanks in advance. .NET 3.5 SP1, Visual Studio 2008 C#.

Sincerely,

tf.rz


I am not sure

However I think you will find that the datarows have been created using the old column definitions.

when you add a new column i doubt it is resizing the arrays for each row to add a column. Although if this was true i would expect an index out of range exceptions.

you need to break that big line of code up so so that it is easier to debug.

IE when you leave the loop does the cell have the value you expect? Or has the assign failed? It will tell you where things are going wrong.

If it is assigning then you lose the data later then you need to expand your search (this could happen if you have somehow assumed a value type was a reference type for instance)

you may finid you need to create a new data table with the correct column definitions and then copy the old data into the new one ... but first you need to establish where it is failing - you are currently doing too much in 1 line of code.

Are you sure the value syou are getting from the combo box are not null?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜