C# Declaring Multiple Instances of the Same Table - c#

I have a table that needs 27 drop down menus in 27 cells to accept user input. Currently I am declaring all 27 of them like this:
DropDownList DropList1 = new DropDownList();
DropList1.ID = "TrendList1";
DropList1.AutoPostBack = true;
DropList1.SelectedIndexChanged += new EventHandler(this.Selection_Change);
DropList1.DataSource = CreateDataSource();
DropList1.DataTextField = "ColorTextField";
DropList1.DataValueField = "ColorValueField";
DropList1.DataBind();
DropDownList DropList2 = new DropDownList();
DropList2.ID = "TrendList2";
DropList2.AutoPostBack = true;
DropList2.SelectedIndexChanged += new EventHandler(this.Selection_Change);
DropList2.DataSource = CreateDataSource();
DropList2.DataTextField = "ColorTextField";
DropList2.DataValueField = "ColorValueField";
DropList2.DataBind();
etc...
However I know there has to be a better way than the brute force code I have written. Unfortunately I am new to web programming and I haven't been able to figure out a better way to do this.
Any advice is appreciated.
Regards.

var data = CreateDataSource();
for(x = 1; x < 28; x++)
{
DropDownList dl = new DropDownList();
dl.ID = "TrendList" + x.ToString();
dl.AutoPostBack = true;
dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
dl.DataSource = data;
dl.DataTextField = "ColorTextField";
dl.DataValueField = "ColorValueField";
dl.DataBind();
// add it to the cell here too
}

Related

Removing the cell borders from header in DataGridView C#

enter image description here
I am using a DataGridView in C# and I wanted to remove the Cell Border of DataGridView.
dataGrid = new DataGridView();
dataGrid.BackgroundColor = Control.DefaultBackColor;
dataGrid.AdvancedColumnHeadersBorderStyle = DataGridViewAdvancedCellBorderStyle.None;
dataGrid.AdvancedRowHeadersBorderStyle = DataGridViewAdvancedCellBorderStyle.None;
dataGrid.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGrid_CellClick);
dataGrid.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(dataGrid_CellMouseEnter);
dataGrid.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(dataGrid_DataBindingComplete);
dataGrid.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGrid_CellPainting);
dataGrid.DataSource = this._lstWorklet;
dataGrid.Columns.Add("Action", "Action");
dataGrid.Size = new System.Drawing.Size(1018, 434);
dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGrid.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
dataGrid.DefaultCellStyle.SelectionBackColor = dataGrid.DefaultCellStyle.BackColor;
dataGrid.DefaultCellStyle.SelectionForeColor = dataGrid.DefaultCellStyle.ForeColor;
dataGrid.ReadOnly = true;
dataGrid.RowTemplate.Height = 48;
dataGrid.ColumnHeadersHeight = 48;
dataGrid.RowHeadersVisible = false;
dataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGrid.ColumnHeadersDefaultCellStyle.Font = new Font(dataGrid.Font, FontStyle.Bold);
dataGrid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGrid.MultiSelect = false;

Cardview Columns using Devexpress

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;

how to use CheckBoxes to select images in visual c#

According to codes below, I can gather my stored images from my SQL server table and put a CheckBox under each of them.My aim is to use these CheckBoxes to select images above them and take an action with another user control (i.e. button for erasing them from DB or copying them to another directory).I tried to create an event handler but cannot achieve with my level of c# knowledge. I kindly ask for a solution and guidance for better understanding.
private void button4_Click(object sender, EventArgs e)
{
PictureBox[] pba = new PictureBox[100];
SqlConnection con4 = new SqlConnection(DBHandler.GetConnectionString());
SqlCommand cmd4 = new SqlCommand("ReadAllImage", con4);
cmd4.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da2 = new SqlDataAdapter(cmd4);
DataSet dt2 = new DataSet("ImageData");
da2.SelectCommand = cmd4;
da2.Fill(dt2);
int a = comboBox1.Items.Count;
byte[] xdata = new byte[0];
CheckBox[] chckbx = new CheckBox[400];
for (var i = 0; i < a; i++)
{
DataRow myRow2 = null;
if (myRow2 == null)
{
myRow2 = dt2.Tables[0].Rows[i];
xdata = (byte[])myRow2["ImageData"];
MemoryStream stream2 = new MemoryStream(xdata);
PictureBox npb = new PictureBox();
npb.Size = new Size(60, 60);
int b = i / 5;
npb.Location = new Point(420 + (i % 5) * 70, 20 + (90 * b));
npb.Image = Image.FromStream(stream2);
npb.SizeMode = PictureBoxSizeMode.StretchImage;
pba[i] = npb;
this.Controls.Add(pba[i]);
CheckBox chckbxx = new CheckBox();
chckbxx.Location = new Point(420 + (i % 5) * 70, 80 + (90 * b));
chckbxx.AutoSize = true;
chckbx[i] = chckbxx;
this.Controls.Add(chckbx[i]);
}
my example output after running the codes.I wanna use the chekckboxes like in this pic.I actually managed to create them like this but couldn't make them related with images above them.
make a custom control holding the image AND the checkbox.
then both are always connected:
logically and programmatically.
samples for custom controls :
https://msdn.microsoft.com/en-us/library/ff723977%28v=expression.40%29.aspx
http://www.codeproject.com/Articles/2016/Writing-your-Custom-Control-step-by-step

Telerik report showing same data for all columns created dynamically

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 });
}
}

Centering specific column in RadListView (Telerik, Winforms)

I'm using a (Telerik) RadListView (Telerik.WinControls.UI.RadListView()) in my project.
The listview has a few columns:
public MainForm()
{
Telerik.WinControls.UI.ListViewDetailColumn newColumn;
InitializeComponent();
newColumn = new ListViewDetailColumn("ID", "ID");
newColumn.Width = 250;
newColumn.Visible = false;
this.MyListView.Columns.Add(newColumn);
newColumn = new ListViewDetailColumn("Name", "Name");
newColumn.Width = 250;
newColumn.Visible = true;
this.MyListView.Columns.Add(newColumn);
newColumn = new ListViewDetailColumn("Description", "Description");
newColumn.Width = 250;
newColumn.Visible = true;
this.MyListView.Columns.Add(newColumn);
newColumn = new ListViewDetailColumn("Costs", "Costs");
newColumn.Width = 250;
newColumn.Visible = true;
this.MyListView.Columns.Add(newColumn);
I'm populating the listview manually by adding ListViewDataItems to it:
foreach (Data.Tablename listEntry in ListOfTableEntries)
{
ListViewDataItem newRow = new ListViewDataItem(listEntry.Id, new string[]{ listEntry.Name, listEntry.Description, listEntry.Costs});
this.MyListView.Items.Add(newRow);
}
So far so good. What I did not find out though is how I can format specific columns that are making up the added row. For example: Right-align the costs.
How can I do that (without also right-aligning all other columns)?
Thanks
Try something along these lines:
private void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
{
if ((e.CellElement).Data.HeaderText == "ID")
{
if ((e.CellElement is DetailListViewDataCellElement))
{
e.CellElement.TextAlignment = ContentAlignment.TopRight;
}
}
if ((e.CellElement).Data.HeaderText == "Name")
{
if ((e.CellElement is DetailListViewDataCellElement))
{
e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
}
//end so on
}
}

Categories