Svexo
Svexo

Reputation: 523

DataGridView only 1 show 1 row

I have a DatagridView with a datasource but my datagridview only draws 1 row.

My code is something along these lines:

Declaration of List

List<Texture> LoadedTextures = new List<Texture>();

Custum Datasource Object

public class Texture
{
    public Image Image { get; set; }
    public string Name { get; set; }
}

Adding Method

private void LoadImage(string FileName)
{
    FileInfo file = new FileInfo(FileName);

    if (LoadedTextures.Count > 0 && 
       LoadedTextures.Where(x => x.Name == file.Name).Count() > 0)
    {
        MessageBox.Show("Already a texture with that name", "error",
           MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    Bitmap newImage = new Bitmap(CurrLevel.TileWidth, CurrLevel.TileHeight);
    using (Graphics gr = Graphics.FromImage(newImage))
    {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(Image.FromFile(FileName), 
           new Rectangle(0, 0, CurrLevel.TileWidth, CurrLevel.TileWidth));
    }
    var text = new Texture
    {
        Image = (Image)newImage,
        Name = file.Name
    };
    LoadedTextures.Add(text);
    if(Tiles.DataSource == null)Tiles.DataSource = LoadedTextures;  
}

When debugging I also noticed that my Datasource contains the loaded elements but the datagridview only draws 1 row though.

P.S : I tried , DataGridView.Refresh() , DataGridView.RefreshEdit() , Assigning the Datasource everytime when a change is made but none of these helped.

Upvotes: 1

Views: 2356

Answers (3)

Svexo
Svexo

Reputation: 523

Using a bindinglist worked for me (used the change event to refresh the list) , Tnx for all the reponses.

Upvotes: 2

SwDevMan81
SwDevMan81

Reputation: 49978

I think your check for if the DataSource is null is probably the issue:

if(Tiles.DataSource == null) Tiles.DataSource = LoadedTextures;

Just remove the check and assign it directly, since the first time you add one Texture, it will change it to non null, then after that it will never update the DataSource again, no matter how many more you add:

Tiles.DataSource = LoadedTextures

If your loading multiple Textures, it might be better to load them all then call Tiles.DataSource = LoadedTextures once, instead of every time you load a new Texture

Upvotes: 0

Ben
Ben

Reputation: 764

Assuming this is a web app, I tried the following to confirm one of my suspicions:

List<int> myList = new List<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < 10; i++)
                myList.Add(i);
        }
    }

    protected void btn_AddItem_Click(object sender, EventArgs e)
    {
        myList.Add(10);
        Console.Write(myList.Count()); //always prints "1"
    }

The list loses all of its items which is what I'm guessing is happening to you. As for how to fix it, I'm not positive but at the very least it narrowed down the source of your problem.

If it's not a web app, then I'm sorry as I'm not familiar with the lifespan of the other types of applications.

Upvotes: 0

Related Questions