user6628729 user6628729 - 3 months ago 18
ASP.NET (C#) Question

display data in grid-view

I try to display data in gridview through linq this is what i try

public void vehcile(string id)
{
Entities1 wr = new Entities1();
List<spchile_Result> sv = wr.spchile(id).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Number",typeof(int));
dt.Columns.Add("RegNo", typeof(string));


foreach(var c in sv)
{
dt.Rows.Add(c.Number,c.RegNo)
}
GridView1.DataSource=;
GridView1.DataBind();
return dt;

}


UPDATE

ok now i am going to minimize no. of code lines and directly bind instead of add columns when i do this show error

public DataTable vehcile(string id)
{
Entities1 wr = new Entities1();
GridView1.DataSource=wr.spvechile(id);
GridView1.DataBind();
return ;
}
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.vehcile(id);
}

}


this show error

Error 10 An object of a type convertible to 'System.Data.DataTable' is required

Error 9 The name 'id' does not exist in the current context
now how i return data and how i set data source?

any solution's?

Answer
      public DataTable vehcile(string id)
    {
        Entities1 wr = new Entities1();
        List<spchile_Result> sv = wr.spchile(id).ToList();
        DataTable dt = new DataTable();
        dt.Columns.Add("Number",typeof(int));
        dt.Columns.Add("RegNo", typeof(string));


        foreach(var c in sv)
        {
            dt.Rows.Add(c.Number,c.RegNo)
        }
        GridView1.DataSource=dt;
        GridView1.DataBind();
        return dt;

    }
Comments