Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

c# - what is the next step, i have the code but i dont know what to do next?

im new to c# and i'm trying to fill my datagridview with data from the database and make a groupby on certain column.

for ex:

i have a datagridview that contains : field, fullname and worker_id.

i want to get the data from database and group them according to the field name.

public void FillFullSchedule()
    {


        using (SqlConnection sqlcon = new SqlConnection(con))
        {
            sqlcon.Open();
            SqlCommand cmd = new SqlCommand("dbo.FullScheduleData", sqlcon);
            //SqlDataReader reader = cmd.ExecuteReader();

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            fullScheduleDG.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                int n = fullScheduleDG.Rows.Add();
                fullScheduleDG.Rows[n].Cells[0].Value = dr[1].ToString();
                fullScheduleDG.Rows[n].Cells[1].Value = dr[0].ToString();
                fullScheduleDG.Rows[n].Cells[2].Value = dr[2].ToString();

            }

        }

    }

no problem with that.

i have the code for grouping the data from this post:

https://stackoverflow.com/a/44807088/10534001

but i dont know how to use it. what should i do ?? i dont know the next step.

is it to add events to cellformating or something like that or what ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Build a class with your 3 desired fields and a BindingList for the data to populate within the DataGrid.

public class yourClassName
{
     public string field { get; set; }
     public string fullname { get; set; }
     public int worker_id { get; set; }
}
public class yourMainClass
{
     public BindingList<yourClassName> yourDataForGrid { get; }
     public yourMainClass(//this is a constructor.  your params go here)
     {
          yourDataForGrid = GetDataForGrid(//your params here)
     }
}

In your load method, create a new object and bind the fields:

yourClassName tempThing;
dgNameOfYourDataGrid.DataSource = new BindingSource(tempThing.yourDataForGrid, null);

Create the method that I called in the constructor that loads the BindingList:

 public static BindingList<yourClassName> GetDataForGrid(//your params here)
 {
      BindingList<yourClassName> tempList = new BindingList<yourClassName>();

      //your sql code to build a dataset goes here
      //foreach row in your dataset
      {
           yourClassName row = new yourClassName();
           row.field = //insert value for field
           row.fullname = //insert value for fullname
           row.worker_id = //insert value for worker_id

           tempList.Add(row);
      }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...