Gridview second page not binding in ASP.NET - c#

I want to bind student register number as in DataTable1 in Gridview1 with positions as in DataTable2 .
But, Gridview1 first page only bind, second page onwards not binding. I give my .net code , DB table structure and Gridview1 display below. If anybody give me solution, I will thankful to you.
C#.Net Code
private void dgvHallSeating()
{
DataSet ds = new DataSet();
DataTable dt;
DataTable dtSeatPositionsAssign = new DataTable();
DataColumn col1;
DataColumn col2;
DataColumn col3;
DataColumn col4;
DataColumn col5;
dt = new DataTable();
col1 = new DataColumn("col1", Type.GetType("System.String"));
col2 = new DataColumn("col2", Type.GetType("System.String"));
col3 = new DataColumn("col3", Type.GetType("System.String"));
col4 = new DataColumn("col4", Type.GetType("System.String"));
col5 = new DataColumn("col5", Type.GetType("System.String"));
dtSeatPositionsAssign.Columns.Add(col1);
dtSeatPositionsAssign.Columns.Add(col2);
dtSeatPositionsAssign.Columns.Add(col3);
dtSeatPositionsAssign.Columns.Add(col4);
dtSeatPositionsAssign.Columns.Add(col5);
Int32 SeatPositionColIndex = 0;
Int32 SeatPositionRowIndex = 0;
DataTable DataTable2 = new DataTable();
DataTable2 = CMSBL.dtecSeatSlot1Positions();
DataTable DataTable1 = CMSBL.rptecSeatSlot1Reports();
int RegNoRowIndex = 0;
int HallSeatPosition = 0;
for (; RegNoRowIndex < 11; RegNoRowIndex++)
{
for (; HallSeatPosition < DataTable2.Rows.Count; )
{
SeatPositionColIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridColumnIndex"]);
SeatPositionColIndex = SeatPositionColIndex - 1;
SeatPositionRowIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridRowIndex"]);
SeatPositionRowIndex = SeatPositionRowIndex - 1;
dtSeatPositionsAssign.Rows.Add();
if (SeatPositionRowIndex >= dtSeatPositionsAssign.Rows.Count || GridView1.Rows.Count <= 5)
{
dtSeatPositionsAssign.Rows.Add();
}
dtSeatPositionsAssign.Rows[SeatPositionRowIndex][SeatPositionColIndex] = DataTable1.Rows[RegNoRowIndex][3].ToString();
break;
}
HallSeatPosition++;
}
GridView1.DataSource = dtSeatPositionsAssign;
GridView1.DataBind();
}
**
DataTable1
**
Datatable2
**
Gridview1 Display

Related

How to display row number in gridview

I use code below but it didint work.. It didnt show me column with that field.
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
I add my other columns in code behind and meaby it's the problem?
OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive);
DataTable dt = new DataTable();
connProdcc1.Open();
sqlArchiveData.Fill(dt);
foreach (DataColumn column in dt.Columns)
{
BoundField field = new BoundField();
field.DataField = column.ColumnName;
field.HeaderText = column.ColumnName.Replace("_"," ");
field.SortExpression = column.ColumnName;
AggregateGridView.Columns.Add(field);
}
AggregateGridView.DataSource = dt;
AggregateGridView.DataBind();
Does anone have any idea how to do this in other way?
I try to this like below but it's counts my row number after all data I Fill() into a DataTable and then add to each row a number
DataColumn columnIndexRow = new DataColumn();
columnIndexRow.DataType = System.Type.GetType("System.Int32");
columnIndexRow.ColumnName = "id";
dt.Columns.Add(columnIndexRow);
for (int i = 0; i < dt.Rows.Count; i++)
{
// your index is in i
var row = dt.NewRow();
row["id"] = i;
dt.Rows.Add(row);
}
ADD row number field to DataTable
OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive);
DataTable dt = new DataTable();
connProdcc1.Open();
sqlArchiveData.Fill(dt);
dt = AutoNumberedTable(dt);
foreach (DataColumn column in dt.Columns)
{
BoundField field = new BoundField();
field.DataField = column.ColumnName;
field.HeaderText = column.ColumnName.Replace("_"," ");
field.SortExpression = column.ColumnName;
AggregateGridView.Columns.Add(field);
}
AggregateGridView.DataSource = dt;
AggregateGridView.DataBind();
private DataTable AutoNumberedTable(DataTable SourceTable)
{
DataTable ResultTable = new DataTable();
DataColumn AutoNumberColumn = new DataColumn();
AutoNumberColumn.ColumnName="S.No.";
AutoNumberColumn.DataType = typeof(int);
AutoNumberColumn.AutoIncrement = true;
AutoNumberColumn.AutoIncrementSeed = 1;
AutoNumberColumn.AutoIncrementStep = 1;
ResultTable.Columns.Add(AutoNumberColumn);
ResultTable.Merge(SourceTable);
return ResultTable;
}
Hope this might help. :)
Note: I haven't tested this code.
Thanks guys! I just find out the answer. I do something like updating a cell at row index and first column
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i][0] = i;
}

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

How to add textBox to Winform dataGridView cells

I want to add textBox to my column cells by for loop...
My code:
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("No");
dt.Columns.Add(dc);
dc = new DataColumn("Item");
dt.Columns.Add(dc);
dc = new DataColumn("Available Stock");
dt.Columns.Add(dc);
dc = new DataColumn("Quantity");
dt.Columns.Add(dc);
dc = new DataColumn("Price");
dt.Columns.Add(dc);
// Define rows
for (int i = 0; i < count; i++)
{
string no = dtr.Rows[i][0].ToString();
string item = dtr.Rows[i][2].ToString() + " " + dtr.Rows[i][1].ToString();
string A_qty = dtr.Rows[i][3].ToString();
string price = dtr.Rows[i][4].ToString();
dt.Rows.Add(no, item, A_qty,"[i want to add text box here] " ,price);
}
dataGridView1.DataSource = dt;
I want to add textBoxes to 4th column and I want access that one by one.
You can add textBox column to DataGridView instead and loop through DataGridView like
DataTable dt=new DataTable();
dt.Columns.Add("No",typeof(int));
dt.Columns.Add("Item",typeof(string));
dt.Columns.Add("quantity",typeof(int));
dt.Columns.Add("Price",typeof(decimal));
//Add row to the datatable
for (int i = 0; i < count; i++)
{
//Not Sure what dtr is you looping through
string no = dtr.Rows[i][0].ToString();
string item = dtr.Rows[i][1].ToString() + " " + dtr.Rows[i][1].ToString();
string A_qty = dtr.Rows[i][2].ToString();
string price = dtr.Rows[i][3].ToString();
dt.Rows.Add(no, item, A_qty, price);
}
//Create New DataGridViewTextBoxColumn
DataGridViewTextBoxColumn textboxColumn=new DataGridViewTextBoxColumn();
//Bind DataGridView to Datasource
dataGridView1.datasource=dt;
//Add TextBoxColumn dynamically to DataGridView
dataGridView1.Columns.Add(textboxColumn);
//Loop through DataGridView
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Do your task here
string fourthColumn = row.Cells[4].Value.toString();
}
Not sure. But hope this code will help you. Just add column of type textbox to your datatable.
DataTable table = new DataTable();
DataColumn col = new DataColumn("Name", typeof(TextBoxBase));
table.Columns.Add(col);

Fill two DataTable in single DataSet

I have single DataSet.xsd file and in this i have two DataTable one is Bill and another is BillInfo now, i am fill writing this code
string qry = "Select * from BillInfo where [BillNumber]='" +txtbill.Text + "' and [Session]='"+CLS.ses+"'";
OleDbDataAdapter da = bm.returnDataAdaptor(qry);
DataSet ds1 = new DataSet();
da.Fill(ds1);
DataSets.Bill ds = new DataSets.Bill();
da.Fill(ds);
DataTable Dt1 = ds1.Tables[0];
DataTable dt = ds.Tables.Add("BillInfo");
dt.Columns.Add("BillNumber", Type.GetType("System.String"));
dt.Columns.Add("CustomerName", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Mobile", Type.GetType("System.String"));
dt.Columns.Add("TotalAmount", Type.GetType("System.String"));
dt.Columns.Add("Packing", Type.GetType("System.String"));
dt.Columns.Add("Tax", Type.GetType("System.String"));
dt.Columns.Add("Transport", Type.GetType("System.String"));
dt.Columns.Add("Additional", Type.GetType("System.String"));
dt.Columns.Add("PaybleAmount", Type.GetType("System.Int32"));
dt.Columns.Add("Session", Type.GetType("System.String"));
dt.Columns.Add("Date1", Type.GetType("System.DateTime"));
dt.Columns.Add("Naration", Type.GetType("System.String"));
dt.Columns.Add("AmountWord", Type.GetType("System.String"));
dt.Columns.Add("Status", Type.GetType("System.String"));
dt.Columns.Add("Balance", Type.GetType("System.Int32"));
for (int i = 0; i < Dt1.Rows.Count; i++)
{
DataRow r = dt.NewRow();
r["BillNumber"] = Dt1.Rows[i][0];
r["CustomerName"] = Dt1.Rows[i][1];
r["City"] = Dt1.Rows[i][2];
r["Mobile"] = Dt1.Rows[i][3];
r["TotalAmount"] = Dt1.Rows[i][4];
r["Packing"] = Dt1.Rows[i][5];
r["Tax"] = Dt1.Rows[i][6];
r["Transport"] = Dt1.Rows[i][7];
r["Additional"] = Dt1.Rows[i][8];
r["PaybleAmount"] = Dt1.Rows[i][9];
r["Session"] = Dt1.Rows[i][10];
r["Date1"] = Dt1.Rows[i][11];
r["Naration"] = Dt1.Rows[i][12];
r["AmountWord"] = Dt1.Rows[i][13];
r["Status"] = Dt1.Rows[i][14];
r["Balance"] = Dt1.Rows[i][15];
dt.Rows.Add(r);
}
string qry1 = "Select * from BillDetails where [BillNumber]='" + txtbill.Text + "'";
OleDbDataAdapter da1 = bm.returnDataAdaptor(qry1);
DataSet ds2 = new DataSet();
da.Fill(ds2);
da.Fill(ds);
DataTable Dt2 = ds2.Tables[0];
DataTable dt2 = ds.Tables.Add("Bill");
dt2.Columns.Add("BillNumber", Type.GetType("System.String"));
dt2.Columns.Add("ProductCode", Type.GetType("System.String"));
dt2.Columns.Add("ProductName", Type.GetType("System.String"));
dt2.Columns.Add("productSize", Type.GetType("System.String"));
dt2.Columns.Add("ProductQuantity", Type.GetType("System.String"));
dt2.Columns.Add("ProductWeight", Type.GetType("System.String"));
dt2.Columns.Add("Unit", Type.GetType("System.String"));
dt2.Columns.Add("ProductPrice", Type.GetType("System.String"));
dt2.Columns.Add("Amount", Type.GetType("System.String"));
dt2.Columns.Add("Date1", Type.GetType("System.DateTime"));
for (int i = 0; i < Dt2.Rows.Count; i++)
{
DataRow r = dt.NewRow();
r["BillNumber"] = Dt2.Rows[i][0];
r["ProductCode"] = Dt2.Rows[i][1];
r["ProductName"] = Dt2.Rows[i][2];
r["productSize"] = Dt2.Rows[i][3];
r["ProductQuantity"] = Dt2.Rows[i][4];
r["ProductWeight"] = Dt2.Rows[i][5];
r["Unit"] = Dt2.Rows[i][6];
r["ProductPrice"] = Dt2.Rows[i][7];
r["Amount"] = Dt2.Rows[i][8];
r["Date1"] = Dt2.Rows[i][9];
dt2.Rows.Add(r);
}
but at the r["ProductCode"] = Dt2.Rows[i][1]; it is showing an exception. That is Column 'ProductCode' does not belong to table BillInfo. Please solve my error.
Replace
DataRow r = dt.NewRow();
with
DataRow r = dt2.NewRow();
You have simply used the wrong table which has no column ProductCode. It must be "Bill" instead of "BillInfo".
Apart from that:
You are open for sql-injection when you use textboxes as input for your sql query. Use parameters instead.
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx
The answer is very clear Column 'ProductCode' does not belong to table BillInfo instead I can see Column 'ProductCode' is in BillDetails.

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