Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a program using a ListView.

I want to change listview to a DataGridView

To populate the list view I have a C# class to connect to the database with the following code:
void IncarcaDateDupaLuna(object sender, EventArgs e)
{
  string CommandText = "select * from ProduseAmanet where Shared='" + Convert.ToInt32("0") + "' AND LaDataDe LIKE \'%" + txtLuna.Text.ToUpper() + "%\' ";
  Program.Connection.CommandText = CommandText;
  Program.Connection.FillDataTable(dtProduseStoc, true);

  AfiseazaProduseStoc(false, null);
  this.lblTotalProduseStoc.Text = "Produse stoc: " + this.listProduseStoc.Items.Count + " produs(e)";
}

void AfiseazaProduseStoc(bool Shared, string CategoryID)
{
  CID.Items.Clear();
  listProduseStoc.Items.Clear();
            
  for (int i = 0; i < dtProduseStoc.Rows.Count; i++)
  {
    Color service = Color.FromArgb(100, 240, 240);
    Color returnatservice = Color.Red;
    Color returprodus = Color.YellowGreen;

    if (dtProduseStoc.Rows[i]["Operatii"].ToString() == "0")
    {
      CID.Items.Add(dtProduseStoc.Rows[i]["IDProdus"]);
      listProduseStoc.Items.Add(new ListViewItem(new string[] { dtProduseStoc.Rows[i]["ContractNumar"].ToString(), 
                                                                dtProduseStoc.Rows[i]["DescriereProdus"].ToString(), 
                                                                dtProduseStoc.Rows[i]["PretAchizitie"].ToString(), 
                                                                dtProduseStoc.Rows[i]["PretVanzare"].ToString(), 
                                                                dtProduseStoc.Rows[i]["AdaugatLaData"].ToString(), 
                                                                dtProduseStoc.Rows[i]["AdaugatDe"].ToString(), 
                                                                dtProduseStoc.Rows[i]["Achizitie"].ToString(), 
                                                                dtProduseStoc.Rows[i]["AlteInformatii"].ToString() }));
       listProduseStoc.Items[i].BackColor = service;
     }
   }
}



I want to populate the dataGridView. Is it possible with the same syntax.

Some suggestions?

[Modified: fixed all of the spelling errors and formatted the code]
Posted
Updated 11-Mar-11 10:27am
v3
Comments
William Winner 11-Mar-11 16:31pm    
Can I just ask....

why in the world did you write this: Convert.ToInt32("0")???
aciobanita 11-Mar-11 16:48pm    
I wrote Convert.ToInt32("0") because evry time i insert a row in data base is ecrypted, was at first time Converter.Encrypt("0"), but i repleced all with convert.toint and never modifi the INT.

1 solution

Assuming -

i)dtProduseStoc is a datatable which you are passing to FillDataTable() method and
ii)FillDataTable() method is working perfectly fine, and
iii) I also guess dtProduseStoc table is declared globally

do as below -

a)Set the AutoGenerateColumns of the gridview you want to bind the data to, as false

b)Then in place of

AfiseazaProduseStoc(false, null);


add a new line as

BindGridview();


c)And at last, implement your BindGridview() method as

void BindGridview()
{
 GridviewId.DataSource = dtProduseStoc ;
 GridviewId.DataBind();
}




HTH,
Rajeev
 
Share this answer
 
Comments
William Winner 11-Mar-11 16:29pm    
From OP:

and inplace of

listProduseStoc.Items.Add(new ListViewItem(new string[] { dtProduseStoc.Rows[i]["ContractNumar"].ToString(), dtProduseStoc.Rows[i]["DescriereProdus"].ToString(), dtProduseStoc.Rows[i]["PretAchizitie"].ToString(), dtProduseStoc.Rows[i]["PretVanzare"].ToString(), dtProduseStoc.Rows[i]["AdaugatLaData"].ToString(), dtProduseStoc.Rows[i]["AdaugatDe"].ToString(), dtProduseStoc.Rows[i]["Achizitie"].ToString(), dtProduseStoc.Rows[i]["AlteInformatii"].ToString() }));


what to use

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900