Can't disable Add New row at bottom of Datagrid

  • Thread starter Thread starter Steven Line
  • Start date Start date
S

Steven Line

Based on this post from Clay Burch and the SyncFusion FAQ website, I
tried to disable my add new row at the bottom of my datagrid. However
the add new row still shows up. When I click on it my app crashes. Can
anybody look at the routine I've included at the bottom and tell me what
I did wrong?
Thanks,
Steve

Clay's Post:

You can do this by getting at a dataview associated with the datagrid,
and
setting its AllowNew member. Here is one way you can do this.


CurrencyManager cm =
(CurrencyManager)this.BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];

//no adding of new rows
((DataView)cm.List).AllowNew = false;

===================================
Clay Burch
Syncfusion, Inc.

My Routine:
private void loadMessagesDataGrid(ref DataTable msgsDataTable)
{
DateTime startDateTime = DateTime.Now.AddDays(-1);
string messagesSelectStr = "SELECT id, fldtimestamp, "+
"payload, direction, state, transNum, port "+
"FROM messages "+
"WHERE port = "+Port+" "+
"AND fldtimestamp > #"+startDateTime+"# "+
"ORDER BY fldtimestamp DESC";

// Create data adapter objects
MessagesDataAdapter = new OleDbDataAdapter(messagesSelectStr,
Db.getConnection());
MessagesDataAdapter.TableMappings.Add("Table1", "messages");

createMessagesInsertAndUpdateCommands(MessagesDataAdapter, Db);

try
{
MessagesDataAdapter.Fill(dgDataSet, "messages");
}
catch (Exception ex)
{
Log.WriteLine("Exception Filling messagesDataAdapter: "+ex.Message);

return;
}

DataGridTableStyle messagesStyle = null;
try
{
messagesStyle = new DataGridTableStyle();
messagesStyle.MappingName = "messages";
}
catch (Exception ex)
{
Log.WriteLine("Exception getting DataGridTableStyle: "+ex.Message);
return;
}

// HERE IS THE FIRST LINE SUGGESTED BY SYNCFUSION
CurrencyManager cm = (CurrencyManager)BindingContext[dgDataSet,
"messages"];
try
{
int numCols = dgDataSet.Tables["messages"].Columns.Count;
DataGridColoredTextBoxColumn aColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
aColumnTextColumn = new DataGridColoredTextBoxColumn(this);
aColumnTextColumn.HeaderText =
dgDataSet.Tables["messages"].Columns.ColumnName;
aColumnTextColumn.MappingName =
dgDataSet.Tables["messages"].Columns.ColumnName;
if(i == 1) // fldtimestamp
{
aColumnTextColumn.Format = "g";
}
messagesStyle.GridColumnStyles.Add(aColumnTextColumn);
}

messagesStyle.GridColumnStyles["fldtimestamp"].Width = 110;
messagesStyle.GridColumnStyles["payload"].Width = 250;
messagesStyle.GridColumnStyles["direction"].Width = 0;
messagesStyle.GridColumnStyles["state"].Width = 75;
messagesStyle.GridColumnStyles["transNum"].Width = 0;
messagesStyle.GridColumnStyles["port"].Width = 0;

messagesDataGrid.TableStyles.Clear();
messagesDataGrid.TableStyles.Add(messagesStyle);

// HERE IS THE SECOND LINE SUGGESTED BY SYNCFUSION
((DataView)cm.List).AllowNew = false;

}
catch (Exception ex)
{
Log.WriteLine("Exception setting up TableStyles: "+ex.Message);
return;
}

// bind messages data grid with messages data table
try
{
msgsDataTable = dgDataSet.Tables["messages"];
messagesDataGrid.DataSource = msgsDataTable;
msgsDataTable.DefaultView.Sort = "fldtimestamp DESC";
}
catch (Exception ex)
{
Log.WriteLine("Exception: "+ex.Message);
}
}
 
Hi Steven,

The suggested solution should be applied after the grid has been bound to
the data set (or data table). I have tested it myself and it worked
perfectly.

By the way, I couldn't find a SetDataBinding call in your code. I beleive it
is necessary after you have retrieved data from the database.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Based on this post from Clay Burch and the SyncFusion FAQ website, I tried
to disable my add new row at the bottom of my datagrid. However the add new
row still shows up. When I click on it my app crashes. Can anybody look at
the routine I've included at the bottom and tell me what I did wrong?
Thanks,
Steve
Clay's Post:
You can do this by getting at a dataview associated with the datagrid, and
setting its AllowNew member. Here is one way you can do this.

CurrencyManager cm =
(CurrencyManager)this.BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
//no adding of new rows
((DataView)cm.List).AllowNew = false;
===================================
Clay Burch
Syncfusion, Inc.
My Routine:
private void loadMessagesDataGrid(ref DataTable msgsDataTable)
{
DateTime startDateTime = DateTime.Now.AddDays(-1);
string messagesSelectStr = "SELECT id, fldtimestamp, "+
"payload, direction, state, transNum, port "+
"FROM messages "+
"WHERE port = "+Port+" "+
"AND fldtimestamp > #"+startDateTime+"# "+
"ORDER BY fldtimestamp DESC";
// Create data adapter objects
MessagesDataAdapter = new OleDbDataAdapter(messagesSelectStr,
Db.getConnection());
MessagesDataAdapter.TableMappings.Add("Table1", "messages");
createMessagesInsertAndUpdateCommands(MessagesDataAdapter, Db);
try
{
MessagesDataAdapter.Fill(dgDataSet, "messages");
}
catch (Exception ex)
{
Log.WriteLine("Exception Filling messagesDataAdapter: "+ex.Message);
return;
}
DataGridTableStyle messagesStyle = null;
try
{
messagesStyle = new DataGridTableStyle();
messagesStyle.MappingName = "messages";
}
catch (Exception ex)
{
Log.WriteLine("Exception getting DataGridTableStyle: "+ex.Message);
return;
}

// HERE IS THE FIRST LINE SUGGESTED BY SYNCFUSION
CurrencyManager cm = (CurrencyManager)BindingContext[dgDataSet,
"messages"];
try
{
int numCols = dgDataSet.Tables["messages"].Columns.Count;
DataGridColoredTextBoxColumn aColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
aColumnTextColumn = new DataGridColoredTextBoxColumn(this);
aColumnTextColumn.HeaderText =
dgDataSet.Tables["messages"].Columns.ColumnName;
aColumnTextColumn.MappingName =
dgDataSet.Tables["messages"].Columns.ColumnName;
if(i == 1) // fldtimestamp
{
aColumnTextColumn.Format = "g";
}
messagesStyle.GridColumnStyles.Add(aColumnTextColumn);
}
messagesStyle.GridColumnStyles["fldtimestamp"].Width = 110;
messagesStyle.GridColumnStyles["payload"].Width = 250;
messagesStyle.GridColumnStyles["direction"].Width = 0;
messagesStyle.GridColumnStyles["state"].Width = 75;
messagesStyle.GridColumnStyles["transNum"].Width = 0;
messagesStyle.GridColumnStyles["port"].Width = 0;
messagesDataGrid.TableStyles.Clear();
messagesDataGrid.TableStyles.Add(messagesStyle);
// HERE IS THE SECOND LINE SUGGESTED BY SYNCFUSION
((DataView)cm.List).AllowNew = false;
}
catch (Exception ex)
{
Log.WriteLine("Exception setting up TableStyles: "+ex.Message);
return;
}
// bind messages data grid with messages data table
try
{
msgsDataTable = dgDataSet.Tables["messages"];
messagesDataGrid.DataSource = msgsDataTable;
msgsDataTable.DefaultView.Sort = "fldtimestamp DESC";
}
catch (Exception ex)
{
Log.WriteLine("Exception: "+ex.Message);
}
}
 
Don't these two lines set the databinding? If not could you explain the
difference?

msgsDataTable = dgDataSet.Tables["messages"];
messagesDataGrid.DataSource = msgsDataTable;

Thanks for your help,
Steve
 
Steven,

I just can say that there IS a difference. I cannot explain the details, but
my experience proves that most likely the SetDataBinding method performs
some additional, but necessary actions. Well, at least you will loose
nothing if you just try it, won't you? ;-)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Steven Line said:
Don't these two lines set the databinding? If not could you explain the
difference?

msgsDataTable = dgDataSet.Tables["messages"];
messagesDataGrid.DataSource = msgsDataTable;

Thanks for your help,
Steve
Hi Steven,

The suggested solution should be applied after the grid has been bound to
the data set (or data table). I have tested it myself and it worked
perfectly.

By the way, I couldn't find a SetDataBinding call in your code. I beleive it
is necessary after you have retrieved data from the database.
 
YOU ARE RIGHT! You're thte best. It now works. Here's the new code
for others to see the solution:

try
{
msgsDataTable = dgDataSet.Tables["messages"];
// messagesDataGrid.DataSource = msgsDataTable; //
removed this line
messagesDataGrid.SetDataBinding(dgDataSet, "messages");
// added this line
msgsDataTable.DefaultView.Sort = "fldtimestamp DESC";

CurrencyManager cm =
(CurrencyManager)messagesDataGrid.BindingContext[dgDataSet, "messages"];
((DataView)cm.List).AllowNew = false; // now this works
fine, before it didn't
Log.WriteLine("did allowNew");
}
catch (Exception ex)
{
Log.WriteLine("Exception: "+ex.Message);
}
 
Back
Top