Silverlight / WPF - Clearing Value of the Column with DataType decimal in WPf DataGrid

Asked By Varsha Ramnani
29-Jul-10 05:48 AM
Hi All,

I m facing a problem with WPF DataGrid.

I m using WPF DataGrid & DataGridTextColumns

i m using DataView as DataSource to grid

i have Columns in DataView that hold Decimal values.

i have set AllowDBNull Property of DataColumn to true to accept Null values but when user clears the Cell it doesnt clear the value & value is not altered.

I tried CellEditEnding event of datagrid & somehow managed to find that the value is cleared so i manually Updated the value of the DataView

 private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
      {
        if (e.EditingElement is TextBox)
        {
          TextBox tb = (TextBox)e.EditingElement;
          if (tb.Text == "")
          {
            ((DataRowView)DataGrid.CurrentCell.Item).Row[ColumnName] = DBNull.Value;
          }
        }
      }  

but then it sets the "value = 0" rather than "null"


I need to set that value to null & clear it as it will clear any string Field.


  Super Man replied to Varsha Ramnani
29-Jul-10 01:08 PM
then try to use like this :)

((DataRowView)DataGrid.CurrentCell.Item).Row[ColumnName] ="" ;
  Varsha Ramnani replied to Super Man
30-Jul-10 12:15 AM
It is a Decimal field as i mentioned before & it will not accept "" string value.

Well Thanx for the reply i actually managed to attained it.

there was no issue with DataTable & DataColumn it accepts DbNull.Value & even sets it but somewhere after that set statement the value of DataTable was getting set to 0, i m not able to find why & where this is happening but i did it using the follwing code :

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
      {
        if (e.EditingElement is TextBox)
        {
          TextBox tb = (TextBox)e.EditingElement;         
          if (tb.Text == "")
          {
            ((DataRowView)e.Row.DataContext).Row[e.Column.SortMemberPath] = DBNull.Value;            
            ((DataRowView)e.Row.DataContext).Row.AcceptChanges();
            ((DataRowView)e.Row.DataContext).Row.Table.AcceptChanges();           
          }
        }     
      }  

i know this is not the correct way to do this but in WPF DataGrid with Numeric Datatype Fields while clearing the values it sets it to 0 rather than DbNull.Value or null.

if anyone has any other Option to do this than pls reply

Thanx
Create New Account
help
how to insert data from datagrid to database in wpf hello frnds how to insert data from datagrid to database in wpf. ints showing index out of range. int dc = GrdBank.Items.Count; int jj = 0; while (dc-1 ! = 0) { DataRowView row = (DataRowView)GrdBank.SelectedItems[jj]; int bid = BankId(); string amtType = row[0].ToString(); string Bankname = row[1 conn; SqlCommand comm; for ( int i = 0; i < dataGrid1.Items.Count-1; i++) { System.Data. DataRowView item = (System.Data. DataRowView )dataGrid1.Items[i]; / / fetch columns string name = item.Row[0].ToString(); int id = int .Parse
Datagrid CellValueChanged in WPF hello frnds i m working in wpf apps . i want to if i change the cell value of datagrid its show the sum. which event i have to use for this operations. Implement currentCellChanged private void dataGrid1_CurrentCellChanged( object sender, EventArgs e) { System.Data. DataRowView item = (System.Data. DataRowView )dataGrid1.Items[0]; int total = int .Parse(item[0].ToString()) + int .Parse(item[1].ToString()); item.Row[3] = total.ToString(); } Are you looking for something like this WPF Grouping with Total Sums http: / / stackoverflow.com / questions / 678690 / how-can-i-create-a-group footer-in-a-wpf-listview-gridview http: / / wpf.codeplex.com / discussions / 36686?ProjectName = wpf Regards. Create New Account keywords
Wpf, Datagrid, Combobox, and Bind. hi friends can anybody help me, i have combobox in wpf form i want bind with datagrid, so when i double click on datagrid on perticular row in datagrid , the data in datagrid should bind with combobox. . . Use the MouseDoubleClick event handler to capture the double click event on the datagrid row, and use this event handler to get the row details. From the row, get
databinding in datagrid view in wpf how to bind data to textbox from datagrid view in wpf through c# code. Step 1 : Write this code in .cs public Form1() { InitializeComponent(); string connString ds = new DataSet (); da.Fill(ds); dataGrid1.DataContext = ds; } Step 2 : set the properties of dataGrid this way: < DataGrid ItemsSource = "{ Binding Path = Tables[ 0 ]}" Margin = "10" x : Name = "dataGrid1" ColumnHeaderHeight = "30" AutoGenerateColumns = "True" CanUserSortColumns = "False"> < / DataGrid > You can get the cell value like this. < StackPanel > < dg DataGrid Name = "dataGrid1" ItemsSource = "{Binding}" CurrentCellChanged = "dataGrid1_CurrentCellChanged" > < / dg:DataGrid > < TextBox Name = "textBox1" > < / TextBox > < TextBox Name = "textBox2