I am using a dataset that was created seperatley and is being used as a reference.
I need to create a gridview with data that comes from the dataset.
Coding is not the ASP.net code, but the C# code.
I just need to make one column of information.
Teacher has not taught us this and is on an assignment. If you can give me a link or type an example that would be great.
one way to doing is to bind the required columns with empty rows to the datatable and then binding the datatable to the girdview...i have given you a sample below
public void GenerateColumns()
{
dtblDummy = new DataTable("dtblDummy");
dtDummyColumn = new DataColumn("FirstName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("LastName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Email");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Login");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Password");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Role");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("RoleId");
dtblDummy.Columns.Add(dtDummyColumn);
}
public void GenerateRows(int intRow)
{
for(int intCounter = intRow; intCounter < intRow; intCounter++)
{
dtDummyRow = dtblDummy.NewRow();
dtDummyRow["FirstName"] = "";
dtDummyRow["LastName"] = "";
dtDummyRow["Email"] = "";
dtDummyRow["Login"] = "";
dtblDummy.Rows.Add(dtDummyRow);
}
dgrdUsers.DataSource = dtblDummy;
dgrdUsers.DataBind();
dtblDummy = null;
dtDummyRow = null;
dtDummyColumn = null;
}
in the above code dgrdUsers is the gridview control, and declare the dummy row, column and datatable above the page load function.
call the above two functions in your page load under ispostback....
dont forget the create the same no of columns as template column in your gridview...
Related
I am creating an app that prints invoices through Microsoft RDLC report.The report showing some data but the tablix is not showing the values which i provide from dataset.Please see my code below and try to solve my problem.
private void InvoiceButton_Click(object sender, EventArgs e)
{
Parameters p = new Parameters();
List<Parameters> lst = new List<Parameters>();
p.date = CurrentDateTimePicker.Text;
p.CustomerName = CustomerTextBox.Text;
if (CartDataGridView.Rows.Count != 0)
{
for (int i = 0; i < CartDataGridView.Rows.Count-1; i++)
{
lst.Add(new Parameters
{
ItemName = CartDataGridView.Rows[i].Cells[1].Value.ToString(),
Price = CartDataGridView.Rows[i].Cells[2].Value.ToString(),
Quantity = CartDataGridView.Rows[i].Cells[3].Value.ToString(),
Company = CartDataGridView.Rows[i].Cells[4].Value.ToString(),
ExpiryDate = CartDataGridView.Rows[i].Cells[5].Value.ToString(),
Total = CartDataGridView.Rows[i].Cells[7].Value.ToString()
// Subtotal = (Convert.ToInt32(CartDataGridView.Rows[i].Cells[7].Value))
});
}
}
InvoiceForm f = new InvoiceForm();
ReportDataSource rd = new ReportDataSource("MyDataSet");
rd.Value = lst;
f.reportViewer1.LocalReport.ReportEmbeddedResource = "CPMSTestPhase.InvoiceReport.rdlc";
f.reportViewer1.LocalReport.DataSources.Add(rd);
ReportParameter[] rptparam = new ReportParameter[]
{
new ReportParameter("Date",p.date),
new ReportParameter("CustomerName",p.CustomerName),
// new ReportParameter("Subtotal",p.Subtotal.ToString()),
};
f.reportViewer1.LocalReport.SetParameters(rptparam);
f.reportViewer1.RefreshReport();
f.ShowDialog();
}
}ere
I try all solutions available on youtube but did not work.
I myself figured out the issue. The issue is with the order of dataset fields and my parameters class properties. They should be the same.
When my form is loaded, a query is run to get all employee records from SQL. Dapper compiles everything, and I put the contents in a List. My DataGridView table is bound to that list. I have a click event that retrieves the EmployeeID from the first column. Now, how do I sort through the List for the EmployeeModel with the matching ID without repeating the query? I will use this to bind other listboxes with the properties of that EmployeeModel. My first thought was to make the Form_Load method public and have it return employeeList, but it's an async method, and I don't know how to handle that. Would that be the right approach? Is the answer altogether different?
EDIT: I just noticed the first two lines are copied and pasted from my inventory initialization. They should read:
var employeeList = await InitializeEmployeeList();
EmployeeGridView.DataSource = employeeList;
public async void Dash_Load(object sender, System.EventArgs e)
{
var inventoryList = await InitializeInventoryList();
InventoryGridView.DataSource = inventoryList;
//Initialize Employee List
var employeeList = await InitializeEmployeeList();
EmployeeGridView.AutoGenerateColumns = false;
EmployeeGridView.ColumnCount=9;
EmployeeGridView.AutoSize = true;
EmployeeGridView.DataSource = employeeList;
EmployeeGridView.Columns[0].HeaderText = "Employee ID";
EmployeeGridView.Columns[0].DataPropertyName = "ID";
EmployeeGridView.Columns[1].HeaderText = "First Name";
EmployeeGridView.Columns[1].DataPropertyName = "FirstName";
EmployeeGridView.Columns[2].HeaderText = "Last Name";
EmployeeGridView.Columns[2].DataPropertyName = "LastName";
EmployeeGridView.Columns[3].HeaderText = "Nickname";
EmployeeGridView.Columns[3].DataPropertyName = "Nickname";
EmployeeGridView.Columns[4].HeaderText = "JobTitle";
EmployeeGridView.Columns[4].DataPropertyName = "JobTitle";
EmployeeGridView.Columns[5].HeaderText = "Forklift";
EmployeeGridView.Columns[5].DataPropertyName = "ForkliftCert";
EmployeeGridView.Columns[6].HeaderText = "AWP";
EmployeeGridView.Columns[6].DataPropertyName = "AWPCert";
EmployeeGridView.Columns[7].HeaderText = "Confined Space";
EmployeeGridView.Columns[7].DataPropertyName = "ConfinedSpaceCert";
EmployeeGridView.Columns[8].HeaderText = "NFPA51b";
EmployeeGridView.Columns[8].DataPropertyName = "NFPA51bCert";
}
{
DataGridViewCell selectedEmployeeCell = EmployeeGridView.CurrentCell;
int selectedEmployeeRow = selectedEmployeeCell.RowIndex;
string selectedEmployeeID = EmployeeGridView.Rows[selectedEmployeeRow].Cells[0].Value.ToString();
}
put a global
public static InvertoryList lst;
after getting data from your method
fill it with your data
if(lst==null)lst=new InvertoryList();
lst=your inventoryList;
then you can reach lst from everywhere in your form.
created a few seconds ago
Hi,
I am creating a web page in asp.net based on data from a database. This should result in a number of tab pages with card views on each tab with 5 columns and a maximum of 20 rows. The tab pages are working, the rows are working but the columns will not change from the default 3 columns.
I have tried setting the columnCount property at different stages, post and pre databinding. This getting frustrating.
I am having problems with setting a card views column count programmatically. I am setting it, have tried to set it in different places but it always goes to the default 3 columns :(
I am using C# in Visual studio 2017.
Here is the code I am using:
SqlDataSource sds = new SqlDataSource();
public string fName;
protected void Page_Load(object sender, EventArgs e)
{
string fid = Request.QueryString["FID"];
sds.ConnectionString = ConfigurationManager.ConnectionStrings["DataBaseConnection"].ToString();
sds.SelectCommand = "select name from [flashboard] where flashboardid = " + fid;
DataView fDet = (DataView)sds.Select(DataSourceSelectArguments.Empty);
fName = fDet.Table.Rows[0]["name"].ToString();
TitleLink.InnerHtml = fName;
sds.SelectCommand = "SELECT flashboardtabid, name FROM [FlashboardTab] WHERE flashboardid = " + fid+" order by SeqNo";
DataView fTab = (DataView)sds.Select(DataSourceSelectArguments.Empty);
TabPage tabDet;
ASPxPageControl tpc = ASPxPageControl1;
ASPxCardView cardGrp;
CardViewTextColumn cName;
CardViewHyperLinkColumn cEvidence;
foreach (DataRow tab in fTab.Table.Rows)
{
tabDet = new TabPage();
tabDet.Text = tab["name"].ToString();
tabDet.Name = "Tab"+tab["flashboardtabid"].ToString();
tabDet.ActiveTabStyle.Width = Unit.Percentage( 80);
cardGrp = new ASPxCardView();
cardGrp.ID = "CardGroup" + tab["flashboardtabid"].ToString() ;
tabDet.Controls.Add(cardGrp);
tpc.TabPages.Add(tabDet);
cardGrp.AutoGenerateColumns = false;
ASPxCardViewPagerSettings cvps = new ASPxCardViewPagerSettings(cardGrp);
cardGrp.EnableTheming = true;
cardGrp.Theme = "SoftOrange";
cvps.Visible = false;
cvps.SettingsTableLayout.ColumnCount = 5;
cvps.SettingsTableLayout.RowsPerPage = 20;
cardGrp.DataSource = GetData("SELECT cardid, Description, EvidencePage, SmartViewid FROM [flashboardcard] WHERE flashboardtabid = "+tab["flashboardtabid"] + " order by SeqNo");
cardGrp.Attributes.Add("Width", "80%");
cardGrp.Attributes.Add("style", "margin:auto");
cName = new CardViewTextColumn();
cName.Name = "Description";
cName.FieldName = "Description";
cEvidence = new CardViewHyperLinkColumn();
cEvidence.Name = "EvidencePage";
cEvidence.FieldName = "EvidencePage";
cEvidence.PropertiesHyperLinkEdit.Text = "Evidence";
cardGrp.Columns.Add(cName);
cardGrp.Columns.Add(cEvidence);
var layoutitem1 = new CardViewColumnLayoutItem(); // cardGrp.CardLayoutProperties.FindColumnItem("EvidencePage");
layoutitem1.ColumnName = "EvidencePage";
layoutitem1.ShowCaption = DevExpress.Utils.DefaultBoolean.False;
layoutitem1.HorizontalAlign = FormLayoutHorizontalAlign.Center;
var layoutitem2 = new CardViewColumnLayoutItem();
layoutitem2.ColumnName = "Description";
layoutitem2.ShowCaption = DevExpress.Utils.DefaultBoolean.False;
layoutitem2.HorizontalAlign = FormLayoutHorizontalAlign.Center;
layoutitem2.ParentContainerStyle.Font.Bold = true;
layoutitem2.ParentContainerStyle.Font.Size = FontUnit.Medium;
cardGrp.CardLayoutProperties.Items.Add(layoutitem2);
cardGrp.CardLayoutProperties.Items.Add(layoutitem1);
cardGrp.DataBind();
}
}
DataView GetData(String queryString)
{
DataView ds = new DataView();
sds.SelectCommand = queryString;
ds = (DataView)sds.Select(DataSourceSelectArguments.Empty);
return ds;
}
To resolve this issue with ColumnCount, I recommend you define it directly in the control's SettingsPager.SettingsTableLayout.ColumnCount property instead of specifying it in a new ASPxCardViewPagerSettings instance:
cardGrp.SettingsPager.SettingsTableLayout.ColumnCount = 5;
cardGrp.SettingsPager.SettingsTableLayout.RowsPerPage = 20;
I am trying to display my List in a ListView using a GridView. I am using dynamic binding as my list view changes. it's gridview for other purposes.
I have already used similar code twice and had no issues. I have checked every variable using a debugger and executed the code step by step and everything seemed normal.
// the code of the function
public void loadPairsIntoListView(object sender, RunWorkerCompletedEventArgs args)
{
XmlComparator xComp = XmlComparator.getInstance();
List<ComparePair> listOfPairs = xComp.getListOfPairs();
multiCompareListOfCompared.ItemsSource = listOfPairs;
multiCompareModelLabel.Content = listOfPairs[0].model.trueName + " " + listOfPairs[0].model.envName.ToUpper();
GridView myGridView = new GridView();
myGridView.ColumnHeaderToolTip = "Objet recap";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("compared.trueName");
gvc1.Header = "nom";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("compared.envName");
gvc2.Header = "environnement";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("anomalies.Count");
gvc3.Header = "Ecarts";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
multiCompareListOfCompared.View = myGridView;
Log.S();
}
// the class i'm trying to bind
public class ComparePair
{
public XmlFile model;
public XmlFile compared;
public List<int> anomalies;
private const int diffOnlyMarge = 10;
/// Methods ...
}
//
I am getting an output where the List is actually binded to the ListView (i can click on and use every row) but the rows are actually empty. I am getting all the three columns with their names but their rows has no "visual content" while still has the object bind to it.
What i am expecting is to see the actual values i have bind to each column.
I hope i have been clear enough. Tell me if you need more precision.
I have created a telerik report using Visual Studio and set the Datasource from the DataTable. I am creating the columns dynamically at runtime using Telerik.Reporting.TableGroup. Now the problem I am having here is that the report showing the same data for all of the fields and when I debug it is setting different fields for different.
The code I am using is as follows:
private void Report4_NeedDataSource(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = SalesReport.reportDataTable;
table1.DataSource = dt;
Telerik.Reporting.HtmlTextBox textboxGroup;
Telerik.Reporting.HtmlTextBox textBoxTable;
table1.ColumnGroups.Clear();
table1.Body.Columns.Clear();
table1.Body.Rows.Clear();
int ColCount = dt.Columns.Count;
for (int i = 0; i <= ColCount - 1; i++)
{
Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
table1.ColumnGroups.Add(tableGroupColumn);
textboxGroup = new Telerik.Reporting.HtmlTextBox();
textboxGroup.Style.BorderColor.Default = Color.Black;
textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
textboxGroup.Value = dt.Columns[i].ColumnName;
textboxGroup.Size = new SizeU(Unit.Inch(1.5), Unit.Inch(0.6));
tableGroupColumn.ReportItem = textboxGroup;
textBoxTable = new Telerik.Reporting.HtmlTextBox();
textBoxTable.Value = "=Fields." + dt.Columns[i].ColumnName;
textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
table1.Body.SetCellContent(0, i, textBoxTable);
table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
}
}
I suggest reading the following article. I found this same problem and my solution was to add unique names to the group column, label, and detail text boxes.
http://www.telerik.com/forums/incorrect-dynamic-table-columns
//Added
tableGroupColumn.Name = "group" + *something_uniquegoeshere*;
//Added
textboxGroup.Name = "label" + *something_uniquegoeshere*;
//Added
textBoxTable.Name = "data" + *something_uniquegoeshere*;
Not enough space in a comment unfortunately but here's my advice/suggestion. I'm not certain about your specific error, however in the past I have had issues when re-using variables. You declare your variable outside the for statement and it is possible that this is what is causing the problem.
private void Report4_NeedDataSource(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = SalesReport.reportDataTable;
table1.DataSource = dt;
//Telerik.Reporting.HtmlTextBox textboxGroup;
//Telerik.Reporting.HtmlTextBox textBoxTable;
table1.ColumnGroups.Clear();
table1.Body.Columns.Clear();
table1.Body.Rows.Clear();
int ColCount = dt.Columns.Count;
for (int i = 0; i <= ColCount - 1; i++)
{
Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
table1.ColumnGroups.Add(tableGroupColumn);
var textboxGroup = new Telerik.Reporting.HtmlTextBox();
textboxGroup.Style.BorderColor.Default = Color.Black;
textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
textboxGroup.Value = dt.Columns[i].ColumnName;
textboxGroup.Size = new SizeU(Unit.Inch(1.5), Unit.Inch(0.6));
tableGroupColumn.ReportItem = textboxGroup;
var textBoxTable = new Telerik.Reporting.HtmlTextBox();
textBoxTable.Value = "=Fields." + dt.Columns[i].ColumnName;
textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
table1.Body.SetCellContent(0, i, textBoxTable);
table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
}
}