custom columns in Datagridview


I have 4 fields in my database, ID, Country, State, City, Memo

I use datasets to fill the data into a Datagridview. Instead of a 4 column datagridview with contents of each of the above fields,

How can I have 1 visible column in this format

City, State + " - " + Memo

ie: a single column in the datagridview with a combination of data from the database fields.
eg:
Orlando, Florida - Henry Jackson stated he had problems with his phone line

Later I would need to filter this DGV based on certain criteria in the hidden columns


Answer this question

custom columns in Datagridview

  • Socrates Kapetaneas

    sort of, but my problem is that instead of a next/previous-one-rec-at-a-time form, I need a continuously scrolling form where all records are displayed. That's why I considered DGV.

    BTW, are you able to get that code project example to work for you it doesn't convert for me.

  • NoEgo

    Thanks Markku. Actually I'm trying to combine City + State + Zip + Memo into 1 single Datagridview column

  • cablehead

    well it kind of would defeat the purpose of a datagridview in a sense. Why must you have it like this Why dont you for example when the user clicks on say a recordID that in a multilined textbox, it would show the proper representable details as above It just would make things much simpler and less confusing as well as potential problems that may arise later on

  • orcmid

    Select City + ', ' + State + ' ' + cstr(Zip) + chr(13) + chr(10) + Memo AS Details from [Addresses]

  • Schnoogs

    I haven't tried and cannot at the moment but what I don't understand is why cant you use a DGV it does have a scroller and will show it if all the records cant be seen in the size of the DGV control. I understand you want to make this custom column where you are combining a few fields together. I believe that if you try to add a column whilst its databound, it may not be possible to add an extra column programmatically, as its databound to a datasource (try it and see what happens)

    if that works, then I guess you can hide the other columns by going through the columns and hiding them (set the visible property to false) then on this extra column of yours get the data you want from the datasource and present it in the way you like.

    Other way of doing it maybe is not to quite bind the dataset to the datagridview but create 1 column in the datagridview, add a row of data and in this row will be your custom row which will be taken from the values from the dataset datatable, like row1 and columns 1 2 and 3 and then take these and place them into the DGV row by creating a new row and adding the values. Example:

     

    object[] theRowData = new object[] {theDataSet.Tables[0].Rows[0][0] + "-" + theDataSet.Tables[0]Rows[0][1]};

    theDGV.Rows.Add(theRowData);



  • treesprite

    Perhaps let me put this another way...

    How can I fill a single DGV column with city, state + " - " + memo

  • Steve_B

    in Access it would be simple to use scrolling continuous forms based on a query or sql. Is there any similar solution in VC#

    eg
    http://www.codeproject.com/cs/miscctrl/ContiuousForms.asp

    This would be a great alternative to using DGV



  • EmmettS

    you can either doing it that way or say, designing the same form - place your labels/textboxes/controls on the form, add a previous/next button and navigate and databind the record to the controls, and filtering the record, getting the next record from the current record position, which could be stored and displayed to the user like record x of y

    does that make sense



  • Whoisit

    According to sql concatenation, joining fields syntax
    http://4h.wsu.edu/4hData/4H/pdf/03sqlselectexamples.pdf

    Select City || ‘, ‘ || State|| ‘ ‘ || Zip from Youth2B
    should result in output : Wausau, WI 54402-6097

    My question is, how can I use datasets to fill a single column DGV with concatenated data from 3 fields...

    When I use this Sql syntax in my this.theCommand.CommandText
    I get the error Invalid use of vertical bars in query expression '[CITY] ||'.


  • Jon Watte

    Thanks. I'm using Access and using the above I get a strange error when I try to fill the data adapter,
    Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

    Would you please have the time to put together a code sample to open a table and fill a single column Datagridview with concatenated data from city, state, zip fields


  • ArcSlayer

    what database are you using, if it's sql the concatenation character is a + , and you typically use the AS clause to give the new column a name i.e.


    SELECT (city+ ', ' + state + ',' + zip) AS mycustomcolumn
    FROM addresses


  • Professor Mustard

    Hi

    1. Create DataGridView and set all DataBinding ect so that there is all those columns in the grid.

    2. Add new unbound column (I use name "NewColumn").

    3. Add CellFormatting event handler something like this:

    private void MyDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    DataGridView grid = MyDataGridView;
    if (e.RowIndex >= 0 && e.ColumnIndex == grid.Columns["NewColumn"].Index)
    {
    try
    {
    DataGridViewRow row = grid.Rows[e.RowIndex];
    e.Value =
    String.Format("{0}, {1} - {2}", row.Cells["City"].Value.ToString(),
    row.Cells[
    "State"].Value.ToString(), row.Cells["Memo"].Value.ToString());
    }
    catch (Exception)
    {
    e.Value =
    "";
    }
    }
    }

    4. Set "City", "State" and "Memo" columns Visible = false

    Hope this help.

    Markku


  • custom columns in Datagridview