GridControl showing no data but empty rows C# - c#

I have a gridcontrol which needs to be populated with some data.
I have created a datatable and populated data in datarow and added to the table. Data is present in table on analysis. Datatable is then set as datasource for grid.
Code is below.
DataTable table = new DataTable();
table.Columns.Add("Tran Date");
table.Columns.Add("Due Date");
table.Columns.Add("Voucher");
table.Columns.Add("Vr No");
table.Columns.Add("Bill Amount");
table.Columns.Add("OutStanding");
table.Columns.Add("Remittance");
table.Columns.Add("Balance");
for (intcount=0;intcount<=BackupFunctions.mdtAgeingTemp.Rows.Count - 1;intcount++)
{
DataRow row = table.NewRow();
row["Tran Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][1];
row["Due Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][2];
row["Voucher"]= BackupFunctions.mdtAgeingTemp.Rows[intcount][3];
row["Vr No"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][4];
row["Bill Amount"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][10];
row["OutStanding"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][5];
row["Remittance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][6];
row["Balance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][7];
table.Rows.Add(row);
}
grdAgeing.DataSource = table;
Its found that 5 rows which is the result is displaying but they are empty. Could you figure out the issue in my code.

Related

How to display specific row of data based on a column value

From this code I am trying to only display data rows that contain the fields "DBY" in the Station_From_Code column and "MAT" in the Station_To_Column. This will then be printed onto a HTML page. These fields are definitely in the datatable but when I run this cod the table is empty. I am not used to working with data tables in C# so apologies for the poor coding.
dynamic schedluedContent = JsonConvert.DeserializeObject(scheduledJson);
JArray items2 = new JArray();
foreach (JObject stops in schedluedContent.stops)
{
DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr["From_Station_Code"] = stops["station_code"];
dr["To_Station_Code"] = stops["station_code"];
dt.Rows.Add(dr);
DataRow[] result = dt.Select("From_Station_Code = 'DBY' AND To_Station_Code = 'MAT'");
dt.Rows.Add(result);
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();
}
It's a long time I have not seen this style of code for working with database tables! EntityFramework comes to change and surely ease the way of working with database and prevent different risks of using SQL commands inside the code.
If you use EF, your code will become something like bellow:
var rows = myDBContext.MyTable.Where(x=>x.From_Station_Code == "DBY" && x.To_Station_Code == "MAT");
GridViewTrainTimes.DataSource = rows;
You can use find match rows and add in new datatable, that contains only your match rows data then you can bind this dt to gridview.
DataTable dt = new Datatable(); // Lets this datatable have data;
Datatable dt2 = new Datatable(); // Copy all matching rows
foreach (DataRow dr in dt.Rows)
{
if (dr["From_Station_Code"].ToString() == "DBY" && dr["To_Station_Code"].ToString() == "MAT")
{
dt2.Rows.Add(dr);
}
}
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();

How do I search data in from datatable based on user input

I have created one datatable:
DataTable dt=new DataTable();
dt.Columns.Add("Country", typeof(string))
dt.Columns.Add("place", typeof(string))
dt.Columns.Add("Price", typeof(string))
dt.Columns.Add("Desc", typeof(string))
and I have inserted some data in this datatable:
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "wizag";
dtrow["Price"] = "7520";
dtrow["Desc"] = "Anywhere";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["Country"] = "India"; //Bind Data to Columns
dtrow["place"] = "Goa";
dtrow["price"] = "4500";
dtrow["Desc"] = "Anything";
dt.Rows.Add(dtrow);
I have bind this datatable with my grid view. I have also added one textbox and one search button in mmy aspx page. My requirement is that I want to search data based on user input e.g like 4500 or wizag, and if country name is the same I want to show it in one row
How can I do this?
I would use a DataView in this case
DataView dv = new DataView(dt);
dv.RowFilter = "price = 4500 AND Country = 'India'";
GridView.DataSource = dv.ToTable();
GridView.DataBind();

How to check if a column has no value (empty) in all rows in data table in c#

I had pushed the excel file into my data table. The columns empty in excel were left empty in data table.
I guess they are not treated as null.
Can anyone help how to check for empty rows in some column in a data table .
//successfully loaded excel into datatable
OleDbDataReader dr= oledbCommand. ExecuteReader ();
dataTable. Load (dr);
Problem- empty cells in columns remained empty in data table also.
Doubt- Are these empty values in datatable are null??
If not how to check whether dataTable.Rows [1][1] is empty or not?
Please help!
You can use linq :
bool IsColumnEmpty = dt.AsEnumerable().All(dr=>string.IsNullOrEmpty( dr["name"]+""));
Test :
DataTable dt = new DataTable("myTable");
dt.Columns.Add("id", typeof (int));
dt.Columns.Add("name", typeof (string));
DataRow row = dt.NewRow();
row["id"] = 1;
//row["name"] = "";
dt.Rows.Add(row);
row = dt.NewRow();
row["id"] = 0;
//row["name"] = "zzz";
dt.Rows.Add(row);
row = dt.NewRow();
row["id"] = 2;
//row["name"] = "222";
dt.Rows.Add(row);
dt.Dump();
bool IsColumnEmpty= dt.AsEnumerable().All(dr=>string.IsNullOrEmpty( dr["name"]+""));
Console.WriteLine (IsColumnEmpty);
Result :
Let's remove // from //row["name"] = "222";
Result :

How to programmatically add rows to DataGrid in C#?

So as the title states, I'm trying to add rows to a DataGrid programmatically using C# but I can't seem to make it work. This is what I have so far.
// I have a DataGrid declared as dg in the XAML
foreach (string s in array) {
int index = 0;
DataGridRow dgRow = new DataGridRow();
foreach (DataGridColumn dgColumn in columns)
{
// Trying to add Text to dgRow here but IDK how
index++;
}
}
I've been googling around and the closest I got to adding anything was to use {column = value} but it just throws me an error. Really out of ideas now though :\
Here's you can do it better way by binding a source to datagridview
// Creating DataSource here as datatable having two columns
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name");
// Adding the rows in datatable
for (int iCount = 1; iCount < 6; iCount++)
{
var row = dt.NewRow();
row["ID"] = iCount;
row["Name"] = "Name " + iCount;
dt.Rows.AddRow(row);
}
DataGridView dgv = new DataGridView();
// Set AutoGenerateColumns true to generate columns as per datasource.
dgv.AutoGenerateColumns = true;
// Finally bind the datasource to datagridview.
dgv.DataSource = dt;
In Case you are using WPF DataGrid
you can bind it like this way-
dgv.DataContext = employeeData.DefaultView;
and in
XAML
<DataGrid Name="dgv" ItemsSource="{Binding}">
//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));
//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };
//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";
//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
//now bind datatable to gridview...
gridview.datasource=dbtable;
gridview.databind();
Regards
did you try?:
int n=5; // number of rows you want to add
dataGridView1.Rows.Add(n);
// you can add (names of the rows) if you have them in your array
//for(int i=0; i<n; i++)
//dataGridView1[0, i].Value = array[i];

Cannot add new Row to Datatable

Merged with Add new Row to DataTable.
I am trying to add two new Rows to a DataTable. My DataSource consist of a Database with one Table (FooTable) and one column (FooName).
See following code. Only the first Row "Mary" is added and displayed on my DataGrid, but not the second row "Jesus". No exception occurs. Why doesn't it work and what is the correct way to add multiple rows dynamically to DataTable?
/* dg1 = DataGrid; ds = DataSet */
dg1.ItemsSource = ds.Tables;
/* dt = DataTable */
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Mary";
dt.Rows.Add(newRow);
/* add another row to data table */
DataRow newRow2 = dt.NewRow();
newRow2["FooName"] = "Jesus";
dt.Rows.Add(newRow2);

Categories

Resources