GridView doesn't show my datasource - c#

I have gridview "gvData" ,i need to fetch the data of my function to gridview :
(I am using c# application)
algorithm.prison obj=new prison();
gvData.DataSource= obj.TitForTat();
After run i have a empty gridview.
Here is my code:
public DataSet TitForTat()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("myt");
dt.Columns.Add(new DataColumn("iteration", typeof(int)));
dt.Columns.Add(new DataColumn("prison1", typeof(string)));
dt.Columns.Add(new DataColumn("prison2", typeof (string)));
prison[] prisons = new prison[2];
prisons[0] = new prison();
prisons[1] = new prison();
//---------------------------
DataRow dr = dt.NewRow();
prisons[0]._state = "c";
prisons[1]._state = valueOfState[rd.Next(0, 1)];
dr["iteration"] = 0;
dr["prison1"] = "c";
dr["prison2"] = prisons[1]._state;
dt.Rows.Add(dr);
//----------------------
for (int i = 1; i <= _iteration; i++)
{
prisons[0]._state = prisons[1]._state;
prisons[1]._state = valueOfState[rd.Next(0, 1)];
DataRow dr1 = dt.NewRow();
dr1["iteration"] =i;
dr1["prison1"] = prisons[0]._state;
dr1["prison2"] = prisons[1]._state;
dt.Rows.Add(dr1);
}
ds.Tables.Add(dt);
return ds;
}

If it's an ASP.NET-webforms GridView-control you need to call DataBind() after you have assigned the DataSource:
gvData.DataSource = obj.TitForTat();
gvData.DataBind();
If it's winforms you have to set the DataMember property if you use a DataSet instead of a DataTable:
gvData.DataSource = obj.TitForTat();
gvData.DataMember = "myt";
... or use a DataTable in the first place:
DataSet ds = obj.TitForTat();
if(ds.Tables.Count > 0)
gvData.DataSource = ds.Tables[0];

Related

gridview only adds some rows at runtime

I have created an editable gridview and I wan to add rows as many rows as I want at runtime. It is working fine but I'm only able to add 9 rows after that I get a Index out of bound exception, where as I'm not setting the limit anywhere in my code.
private void SetInitialRow()
{
DataTable dtable = new DataTable();
DataRow dr = null;
dtable.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dtable.Columns.Add(new DataColumn("Column1", typeof(string)));
dtable.Columns.Add(new DataColumn("Column2", typeof(string)));
dtable.Columns.Add(new DataColumn("Column3", typeof(string)));
dtable.Columns.Add(new DataColumn("Column4", typeof(string)));
dtable.Columns.Add(new DataColumn("Column5", typeof(string)));
dtable.Columns.Add(new DataColumn("Column6", typeof(string)));
dtable.Columns.Add(new DataColumn("Column7", typeof(string)));
dtable.Columns.Add(new DataColumn("Column8", typeof(string)));
dr = dtable.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dr["Column6"] = string.Empty;
dr["Column7"] = string.Empty;
dr["Column8"] = string.Empty;
dtable.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dtable;
GridView1.DataSource = dtable;
GridView1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = Gtb_ffcode.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = Gtb_terr.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = GddlTeam.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = Gtb_fromdt.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = Gtb_todt.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = Gddl_desg.Text;
dtCurrentTable.Rows[i - 1]["Column7"] = Gddl_role.Text;
dtCurrentTable.Rows[i - 1]["Column8"] = Gtb_manager.Text;
drCurrentRow["Column1"] = Gtb_ffcode.Text = maxcode; // write ff code to the new generating row
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");
Gtb_ffcode.Text = dt.Rows[i]["Column1"].ToString();
Gtb_terr.Text = dt.Rows[i]["Column2"].ToString();
GddlTeam.Text = dt.Rows[i]["Column3"].ToString();
Gtb_fromdt.Text = dt.Rows[i]["Column4"].ToString();
Gtb_todt.Text = dt.Rows[i]["Column5"].ToString();
Gddl_desg.Text = dt.Rows[i]["Column6"].ToString();
Gddl_role.Text = dt.Rows[i]["Column7"].ToString();
Gtb_manager.Text = dt.Rows[i]["Column8"].ToString();
rowIndex++;
}
}
}
}

How to create datatable in C#?

I tried create and fill a simple DataTable
Result of this code :
Order Driver
1
1
But I want to show on same row like this :
Order Driver
1 1
Where is my wrong in this code ?
public DataTable MethodName(string Param)
{
DataRow dr;
DataTable dt= new DataTable();
dt.Columns.Add("Order", Type.GetType("System.Int32"));
dt.Columns.Add("Driver", Type.GetType("System.Int32"));
if(AnotherMethod1(Param))
{ dr = dt.NewRow();
dr["Order"] = 1;
dt.Rows.Add(dr);
}
else
{ dr = dt.NewRow();
dr["Order"] = 0;
dt.Rows.Add(dr);
}
if(AnotherMethod2(Param))
{ dr = dt.NewRow();
dr["Driver"] = 1;
dt.Rows.Add(dr);
}
else
{ dr = dt.NewRow();
dr["Driver"] = 0;
dt.Rows.Add(dr);
}
return dt;
}
You're adding a row every time you set value to a different column.
Try this:
public DataTable MethodName(string Param)
{
DataTable dt = new DataTable();
dt.Columns.Add("Order", typeof(Int32));
dt.Columns.Add("Driver", typeof(Int32));
DataRow dr = dt.NewRow();
dr["Order"] = AnotherMethod1(Param) ? 1 : 0;
dr["Driver"] = AnotherMethod2(Param) ? 1 : 0;
dt.Rows.Add(dr);
return dt;
}
public DataTable MethodName(string Param)
{
DataRow dr;
DataTable dt= new DataTable();
dt.Columns.Add("Order", Type.GetType("System.Int32"));
dt.Columns.Add("Driver", Type.GetType("System.Int32"));
dr = dt.NewRow();
if(AnotherMethod1(Param))
{
dr["Order"] = 1;
}
else
{
dr["Order"] = 0;
}
if(AnotherMethod2(Param))
{
dr["Driver"] = 1;
}
else
{
dr["Driver"] = 0;
}
dt.Rows.Add(dr);
return dt;
}

Combine two non-identical rows of a datatable

I have two datatables dt1 and dt2 ,where as dt1 contains of a single column "ID" and dt2 contains of a single column "NAME". My requirement is to combine both the datatables and create a new datatable (ie)
whereas as mentioned above i need to get the datas of both the datatable (dt1 & dt2 ) and need to create a new datatable.
You can do something like this
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("id", typeof(Int32));
dt2.Columns.Add("Name", typeof(String));
DataRow dr = dt1.NewRow();
dr["id"] = 1;
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["id"] = 2;
dt1.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "XXX";
dt2.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "YYY";
dt2.Rows.Add(dr);
DataTable dt3 = new DataTable();
dt3.Columns.Add("id", typeof(Int32));
dt3.Columns.Add("Name", typeof(String));
for (int i = 0; i < dt1.Rows.Count; i++)
{
dr = dt3.NewRow();
dr["id"] = dt1.Rows[i]["id"];
dr["name"] = dt2.Rows[i]["name"];
dt3.Rows.Add(dr);
}
IF you want to combine to DataTables, you can do the following:
DataTable table1 = new DataTable("Items");
DataColumn column1 = new DataColumn("id", typeof(System.Int32));
table1.Columns.Add(column1);
DataTable table2 = new DataTable("details");
DataColumn column = new DataColumn("Name", typeof(System.String));
table1.Columns.Add(column);
table1.Merge(table2); //table1 will have 2 columns after executing this line

Sort DataGridView according particular column before add

I have DataGridView that i am adding items via DataTable
This items read from XML file and inside this XML file,
those items not sorted and i want to sort is before add to my DataGridView
private void UpdateDataGdirView(List<Vendor> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column4", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column5", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column6", System.Type.GetType("System.Boolean"));
DataRow dr;
foreach (Vendor vendor in list)
{
dr = dt.NewRow();
dr["Column1"] = vendor.IsVendorChecked;
dr["Column2"] = vendor.Number;
dr["Column3"] = vendor.Name;
dr["Column4"] = vendor.Size;
dr["Column5"] = vendor.Path;
dr["Column6"] = vendor.Path2;
dt.Rows.Add(dr);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.Invoke((MethodInvoker)delegate { dataGridView1.DataSource = dt; });
}
you can use
dt.defaultview.sort="columnname asc/desc"
and then bind the datatable to its destination.
I think you should use DataView for this. Like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
EnumerableRowCollection<DataRow> query =
from order in orders.AsEnumerable()
orderby order.Field<decimal>("TotalDue")
select order;
DataView view = query.AsDataView();
bindingSource1.DataSource = view;
Follow this link : Query on DataView
OR
You can use like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
DataView dv = new DataView(orders);
dv.Sort = "TotalDue";
dataGridView1.DataSource = dv;

how to bind a datagrid?

i tried to bind a datagrid but there is a problem in binding of my datagrid..
C# code
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
DataRow dr = dt.NewRow();
dr[0] = item.Key.ToString();
dr[1] = item.Value.ToString();
dt.Rows.Add(dr);
}
grdCategory.DataSource = ds;
grdCategory.DataMember = "Source";
grdCategory.DataTextField = "Desc";
grdCategory.DataValueField = "ID";
grdCategory.DataBind();
Thanks
well... try to post the error that you are getting from this...
but... if you don't need the DataSet you could just do like this...
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
grdCategory.DataSource = lstValue;
grdCategory.AutoGenerateColumns = true; //not sure that's the property
grdCategory.DataBind();

Categories