Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
L

Lisa Jorgensen

@Lisa Jorgensen
About
Posts
10
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • DataSet does not support System.Nullable
    L Lisa Jorgensen

    You need to get the underlying type, e.g.:

    // Get PropertyInfo[] properties from T
    foreach (PropertyInfo propInfo in properties)
    {
    Type propType = propInfo.PropertyType;
    if (propType.IsGenericType &&
    propType.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
    propType = Nullable.GetUnderlyingType(propType);
    }
    // Create DataColumn from propType, etc. ...
    }

    C# java business help question learning

  • Help! Datagridview
    L Lisa Jorgensen

    It sounds like you want to set the DataGridViewComboBoxColumn's DisplayStyleForCurrentCellOnly property to True.

    C# help tutorial

  • Datagridview sort by 2 Column??? [modified]
    L Lisa Jorgensen

    Use a BindingSource to bind your table to the DataGridView, then use the BindingSource's Sort property to specify the columns and directions to sort. E.g.:

    bindingSource.Sort = "username, filename";

    You can also specify the direction of the sort, e.g.:

    bindingSource.Sort = "username ASC, filename DESC";

    C# help tutorial question

  • Setting a default value for a datagridviewtextbox column
    L Lisa Jorgensen

    There's more than one way to approach this. One way is to set the DataGridViewTextBoxColumn's DefaultValue property to the current date. This means that all rows with an unspecified value in that column will get the current date. One problem is that the date will remain the same even if the date changes while the application is still running (i.e. new rows will still get the no-longer-current date). Another approach is to use the DataGridView's DefaultValuesNeeded event to set the current date, e.g. (if the target DataGridViewTextBoxColumn is named "DateColumn"):

    private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
    e.Row.Cells["DateColumn"].Value = DateTime.Now.ToShortDateString();
    }

    This affects only new rows; existing rows will not be updated. If you do both, then existing rows with an unspecified value will get the date on which the application began running and new rows will get the current date.

    C# question

  • Terminating an Application ?
    L Lisa Jorgensen

    First, I gave you bad (or at least incomplete) advice. I meant to say to use the Application.Idle event rather than the Load event. Calling Close from the Load event can cause a memory leak. You could do something like:

    public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();

        // Code to determine whether to close form   
        
        if (ShouldClose)
        {
            Application.Idle += new EventHandler(Application\_Idle);
        }
    }   
    
    
    void Application\_Idle(object sender, EventArgs e)
    {
        this.close();
    }
    

    }

    The Hide and Close methods don't work in the constructor because the Form is still being constructed. And in any case you don't want to close the form at this point, assuming the calling code looks something like:

    Application.Run(new MainForm());

    If MainForm is closed and disposed before it is constructed, then Application.Run will throw an ObjectDisposedException.

    C# csharp question

  • Terminating an Application ?
    L Lisa Jorgensen

    Add a handler for MainForm's Load event, and execute this.Close() inside that handler. E.g.:

    private void MainForm_Load(object sender, System.EventArgs e)
    {
    // Insert code to determine whether logfile is
    // available.

    if (LogFileIsNotAvailable)
    {
        this.Close();
    }
    

    }

    C# csharp question

  • How to force datagrid to display DataColumn caption in column header
    L Lisa Jorgensen

    The DataGrid uses the DataColumn's ColumnName as the column caption. The ColumnName defaults to Column1, Column2, etc., so that's why the code shows "Column1" as the caption. If you set the DataColumn's ColumnName to "Caption", then that will be the caption displayed in the DataGrid.

    C# tutorial question

  • how to use Store Procedure in VC# 2005 ?
    L Lisa Jorgensen

    When you create your SqlCommands, set the CommandType to CommandType.StoredProcedure. Also add the parameters needed by the stored procedure. E.g. something like this:

    using (SqlConnection mySqlConnection = getConnection())
    {
    SqlCommand insertCommand = new SqlCommand("[dbo].[MY_INSERT_PROCEDURE]");
    insertCommand.CommandType = CommandType.StoredProcedure;
    insertCommand.Parameters.Add("@StringField", SqlDbType.VarChar);
    insertCommand.Parameters["@StringField"].Value = "Nice new string";
    insertCommand.Connection = mySqlConnection;
    insertCommand.ExecuteNonQuery();
    }

    C# tutorial csharp question announcement

  • DataGridView and new rows
    L Lisa Jorgensen

    One approach is to add a handler for the BindingSource's AddingNew event. E.g.:

    InitializeGrid()
    {
    // Create sample List of custom objects. First object uses a non-default
    // constructor, the second uses the default (no reason, just seemed
    // like something to do in the example).
    List<CustomItem> sourceList = new List<CustomItem>();
    CustomItem firstItem = new CustomItem("Special content");
    sourceList.Add(firstItem);
    CustomItem nextItem = new CustomItem();
    sourceList.Add(nextItem);

    // Create the BindingSource, add the list, and add an event handler
    // for the AddingNew event.
    BindingSource bindingSource = new BindingSource();
    bindingSource.DataSource = sourceList;
    bindingSource.AddingNew += new AddingNewEventHandler(bindingSource\_AddingNew);
    
    // Bind to the DataGridView.
    dataGridView.DataSource = bindingSource;
    

    }

    private void bindingSource_AddingNew(object sender, AddingNewEventArgs e)
    {
    // Use non-default constructor for the new object that will
    // be the source for the new row.
    e.NewObject = new CustomItem("Not default content");
    }

    An alternative is to use the DataGridView's DefaultValuesNeeded event, but the object has already been constructed by the time this event fires.

    C# question

  • DatagridView Cell datatype validation on edit
    L Lisa Jorgensen

    Here is one approach:

    void repriceQuedataGridView1_CellParsing(object sender,
    DataGridViewCellParsingEventArgs e)
    {
    try
    {
    // Validate entered value by converting to column's type. Note that
    // Convert.ChangeType doesn't handle Nullable types. Also, DataGridView
    // doesn't support setting the value to null within the CellParsing
    // event. To work around this requires creating a derived cell that
    // overrides the ParseFormattedValue method. For this example, assume
    // that the user should not enter nulls.

        object convertedValue = Convert.ChangeType(e.Value, e.DesiredType);
    }
    catch
    {
        // Validation failed. Alert user.
    
        MessageBox.Show("Please try again. Expected type is " + 
                        e.DesiredType.ToString());
    
        // The CurrentCell still contains the pre-edit value. If the 
        // previous value was a null, then restoring the null will lead
        // to another exception (because still in CellParsing event). 
        // Attempt to work around the problem by setting the current cell's 
        // value to an empty string. This works for some types (e.g. DateTime) 
        // but not for others (e.g. Double).
    
        DataGridView gridView = (DataGridView)sender;
    
        if (Convert.IsDBNull(gridView.CurrentCell.Value))
        {
            gridView.CurrentCell.Value = string.Empty;
        }
    
        // Set arg value to current cell's value and mark parsing applied.
    
        e.Value = gridView.CurrentCell.Value;
        e.ParsingApplied = true;
    }
    

    }

    Hope this helps.

    C# csharp
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups