简体   繁体   中英

How can I add values in a specific column for every row in wpf?

I am a new bee in the world of wpf programming this is my first ever post here. I have been learning a lot through the stackoverflow platform (thanks to each and every individual here). My current problem is making me bang my head on a wall as I am not able to find a solution for it. I am doing a project in which I populate a datagrid from an oracle query result using data set as ItemsSource of the datagrid. It works fine but the problem is, I want to have an " additional column " in my datagrid (in other words I want to add a column manually). This column is not being populated by the dataset. Instead, I want to add/insert values in the manually added column based on one of the auto-populated column.

For example: if I have a value " A6 " in column1, the manually added column should display " Actuator " in the correspondent cell. Please help me with any possible solution you know. I'd be grateful!! Idk if my question made any sense or is it too broad. Bear with me if you can't understand anything and feel free to ask. Thank you.

My XAML:

<DataGrid x:Name="datagrid1" FontSize="12" FontWeight="Bold" IsReadOnly="True" 
                    SelectionUnit="FullRow" SelectionMode="Single"                               
                    VerticalAlignment="Bottom" HorizontalAlignment="Left" Height="Auto" Width="985" Margin="1,5,0,0"
                    CanUserResizeColumns="True" CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False" RowHeight="30"
                    CanUserReorderColumns="False" CanUserSortColumns="False" ColumnHeaderHeight="30" ColumnWidth="*">

My Codebehind:

using (OracleCommand cmd = new OracleCommand("SELECT this, that, these, FROM table "
                                                     + "WHERE somecolumn=:somecolumn", connection))
        {
            cmd.CommandType = CommandType.Text;
            OracleParameter parm = new OracleParameter();
            cmd.Parameters.Add("somecolumn", input.Text);
            OracleDataAdapter oda = new OracleDataAdapter(); 
            oda.SelectCommand = cmd;
            DataSet ds = new DataSet(); 
            oda.Fill(ds, "table");
            datagrid1.ItemsSource = ds.Tables["table"].DefaultView;
            DataGridTextColumn column = new DataGridTextColumn(); //here I am able to add the column to the datagrid but not able to insert values in this column             
            datagrid1.Columns.Add(column);

I suggest you try it this way:

//...
DataSet ds = new DataSet(); 
oda.Fill(ds, "table");
var table = ds.Tables["table"];
table.Columns.Add("Columnname",typeof(string));
table.Columns["Columname"].DefaultValue = "Actuator";
datagrid1.ItemsSource = table.DefaultView;

If you want to set rows different you can go like this:

table.Rows[0]["Columnname"] = "somewhat";

Hope this helps after we discuessed in comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM